結果
問題 | No.1013 〇マス進む |
ユーザー | fine |
提出日時 | 2020-03-21 00:24:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 93 ms / 2,000 ms |
コード長 | 5,648 bytes |
コンパイル時間 | 2,113 ms |
コンパイル使用メモリ | 184,628 KB |
実行使用メモリ | 21,428 KB |
最終ジャッジ日時 | 2024-05-09 10:12:44 |
合計ジャッジ時間 | 7,121 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
8,192 KB |
testcase_01 | AC | 4 ms
8,136 KB |
testcase_02 | AC | 3 ms
8,448 KB |
testcase_03 | AC | 5 ms
8,260 KB |
testcase_04 | AC | 4 ms
8,260 KB |
testcase_05 | AC | 4 ms
8,320 KB |
testcase_06 | AC | 4 ms
8,320 KB |
testcase_07 | AC | 4 ms
8,192 KB |
testcase_08 | AC | 4 ms
8,388 KB |
testcase_09 | AC | 3 ms
8,448 KB |
testcase_10 | AC | 3 ms
8,320 KB |
testcase_11 | AC | 4 ms
8,276 KB |
testcase_12 | AC | 4 ms
8,320 KB |
testcase_13 | AC | 6 ms
8,648 KB |
testcase_14 | AC | 39 ms
14,604 KB |
testcase_15 | AC | 75 ms
18,344 KB |
testcase_16 | AC | 58 ms
17,508 KB |
testcase_17 | AC | 31 ms
13,580 KB |
testcase_18 | AC | 38 ms
14,912 KB |
testcase_19 | AC | 24 ms
12,160 KB |
testcase_20 | AC | 81 ms
20,624 KB |
testcase_21 | AC | 28 ms
13,288 KB |
testcase_22 | AC | 36 ms
15,148 KB |
testcase_23 | AC | 14 ms
10,312 KB |
testcase_24 | AC | 71 ms
21,212 KB |
testcase_25 | AC | 69 ms
20,804 KB |
testcase_26 | AC | 16 ms
10,240 KB |
testcase_27 | AC | 28 ms
13,032 KB |
testcase_28 | AC | 14 ms
10,368 KB |
testcase_29 | AC | 73 ms
20,536 KB |
testcase_30 | AC | 23 ms
12,416 KB |
testcase_31 | AC | 45 ms
16,348 KB |
testcase_32 | AC | 36 ms
14,372 KB |
testcase_33 | AC | 34 ms
14,112 KB |
testcase_34 | AC | 54 ms
17,892 KB |
testcase_35 | AC | 55 ms
17,280 KB |
testcase_36 | AC | 7 ms
8,904 KB |
testcase_37 | AC | 7 ms
8,832 KB |
testcase_38 | AC | 61 ms
18,856 KB |
testcase_39 | AC | 19 ms
11,520 KB |
testcase_40 | AC | 59 ms
17,624 KB |
testcase_41 | AC | 14 ms
10,240 KB |
testcase_42 | AC | 32 ms
14,004 KB |
testcase_43 | AC | 22 ms
11,648 KB |
testcase_44 | AC | 40 ms
14,336 KB |
testcase_45 | AC | 34 ms
13,448 KB |
testcase_46 | AC | 19 ms
10,624 KB |
testcase_47 | AC | 34 ms
14,108 KB |
testcase_48 | AC | 37 ms
15,004 KB |
testcase_49 | AC | 51 ms
17,604 KB |
testcase_50 | AC | 47 ms
16,356 KB |
testcase_51 | AC | 42 ms
15,400 KB |
testcase_52 | AC | 10 ms
9,436 KB |
testcase_53 | AC | 13 ms
10,240 KB |
testcase_54 | AC | 48 ms
17,356 KB |
testcase_55 | AC | 17 ms
10,692 KB |
testcase_56 | AC | 70 ms
19,824 KB |
testcase_57 | AC | 48 ms
15,944 KB |
testcase_58 | AC | 93 ms
21,344 KB |
testcase_59 | AC | 92 ms
21,320 KB |
testcase_60 | AC | 83 ms
21,428 KB |
testcase_61 | AC | 81 ms
21,416 KB |
testcase_62 | AC | 20 ms
8,260 KB |
testcase_63 | AC | 5 ms
8,004 KB |
testcase_64 | AC | 3 ms
8,084 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAX_V = 100000; int V; vector<int> G[MAX_V]; vector<int> rG[MAX_V]; vector<int> vs; bool used[MAX_V]; int cmp[MAX_V]; void add_edge(int from, int to) { G[from].push_back(to); rG[to].push_back(from); } void dfs(int v) { used[v] = true; for (int i = 0; i < G[v].size(); i++) { if (!used[G[v][i]]) dfs(G[v][i]); } vs.push_back(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (int i = 0; i < rG[v].size(); i++) { if (!used[rG[v][i]]) rdfs(rG[v][i], k); } } int scc() { memset(used, 0, sizeof(used)); vs.clear(); for (int v = 0; v < V; v++) { if (!used[v]) dfs(v); } memset(used, 0, sizeof(used)); int k = 0; for (int i = (int)vs.size() - 1; i >= 0; i--) { if (!used[vs[i]]) rdfs(vs[i], k++); } return k; } void dfs_end(int cur, ll rest, const vector< vector<int> >& g, int n, const vector<int>& p, vector<ll>& ans, vector<int>& path) { path.push_back(cur); for (int prv : g[cur]) { if (rest > 0) ans[prv] = ans[cur] - n + prv + 1; else ans[prv] = ans[cur] - path[-rest] - 1 + prv + 1; dfs_end(prv, rest - 1, g, n, p, ans, path); } path.pop_back(); } void dfs_loop(int cur, ll rest, const vector< vector<int> >& g, const int n, const vector<int>& p, vector<ll>& ans, vector<ll>& memo, vector<int>& loop_src, vector<int>& path) { if (cur < n) { memo[cur] = rest; path.push_back(cur); } for (int prv : g[cur]) { if (rest > 0) { ans[prv] = (cur >= n ? 0 : ans[cur]) + prv + 1; if (cur < n) loop_src[prv] = loop_src[cur]; } else ans[prv] = ans[cur] - path[-rest] - 1 + prv + 1; dfs_loop(prv, rest - 1, g, n, p, ans, memo, loop_src, path); } if (cur < n) path.pop_back(); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; ll K; cin >> n >> K; vector<int> p(n); for (int i = 0; i < n; ++i) { cin >> p[i]; --p[i]; } if (K == 1) { for (int i = 0; i < n; ++i) { cout << p[i] + 1 + i + 1 << "\n"; } return 0; } V = n; for (int i = 0; i < n; ++i) { if (p[i] + 1 == n) { continue; } add_edge(p[i], p[(i + p[i] + 1) % n]); } int sz = scc(); vector<int> cnt(sz, 0); for (int i = 0; i < n; ++i) { ++cnt[cmp[i]]; } map<int, int> loop_comp_ids; for (int i = 0; i < sz; ++i) { if (cnt[i] > 1) { int hoge = loop_comp_ids.size(); loop_comp_ids[i] = hoge; } } vector< vector<int> > g(n + loop_comp_ids.size()); for (int i = 0; i < n; ++i) { int to; if (loop_comp_ids.find(cmp[i]) == loop_comp_ids.end()) { to = i; } else { to = n + loop_comp_ids[cmp[i]]; } for (int j : G[i]) { if (cmp[i] == cmp[j]) continue; int from; if (loop_comp_ids.find(cmp[j]) == loop_comp_ids.end()) { from = j; } else { from = n + loop_comp_ids[cmp[j]]; } g[from].push_back(to); } } vector<ll> ans(n, -1); ans[n - 1] = n * K; vector<int> path; dfs_end(n - 1, K, g, n, p, ans, path); //cerr << loop_comp_ids.size() << "::\n"; vector<ll> memo(n, -1); for (auto& foo : loop_comp_ids) { int loop_cmp = foo.first; vector<int> loops; vector<int> dict_loops(n, -1); vector<int> loop_src(n, -1); for (int i = 0; i < n; ++i) { if (cmp[i] == loop_cmp) { loop_src[i] = i; memo[i] = K; ans[i] = 0; if (loops.empty()) { int tar = i; while (tar != -1) { dict_loops[tar] = loops.size(); loops.push_back(tar); for (int nex : G[tar]) { if (nex == i) { tar = -1; break; } else if (cmp[nex] == loop_cmp) { tar = nex; break; } } } } continue; } for (int j : G[i]) { if (cmp[j] == loop_cmp) { loop_src[i] = j; break; } } } int loop_size = loops.size(); vector<ll> sum_loops(2 * loop_size + 1, 0); for (int i = 0; i < loop_size; ++i) { sum_loops[i + 1] = sum_loops[i] + loops[i] + 1; } for (int i = 0; i < loop_size; ++i) { sum_loops[i + 1 + loop_size] = sum_loops[i + loop_size] + loops[i] + 1; } path.clear(); dfs_loop(n + foo.second, K, g, n, p, ans, memo, loop_src, path); for (int i = 0; i < n; ++i) { if (loop_src[i] == -1) continue; ll hoge = memo[i] / loop_size * sum_loops[loop_size]; memo[i] %= loop_size; hoge += sum_loops[dict_loops[loop_src[i]] + memo[i]] - sum_loops[dict_loops[loop_src[i]]]; ans[i] += hoge; } } for (int i = 0; i < n; ++i) { //assert(ans[p[i]] != -1); cout << ans[p[i]] + i + 1 << "\n"; } return 0; }