結果

問題 No.1950 片道きゃっちぼーる
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-05-21 00:12:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,544 ms / 3,000 ms
コード長 2,804 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 183,876 KB
実行使用メモリ 52,484 KB
最終ジャッジ日時 2023-10-20 15:08:07
合計ジャッジ時間 34,685 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1,517 ms
52,484 KB
testcase_04 AC 1,520 ms
39,988 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2,518 ms
41,012 KB
testcase_07 AC 441 ms
21,412 KB
testcase_08 AC 439 ms
21,416 KB
testcase_09 AC 1,523 ms
35,124 KB
testcase_10 AC 1,531 ms
34,592 KB
testcase_11 AC 1,519 ms
34,592 KB
testcase_12 AC 1,511 ms
34,592 KB
testcase_13 AC 2,147 ms
35,824 KB
testcase_14 AC 2,445 ms
29,572 KB
testcase_15 AC 1,704 ms
37,768 KB
testcase_16 AC 2,544 ms
34,760 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 977 ms
30,568 KB
testcase_19 AC 1,728 ms
36,336 KB
testcase_20 AC 440 ms
21,416 KB
testcase_21 AC 2,348 ms
30,700 KB
testcase_22 AC 2,222 ms
29,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.05.21 00:12:42
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


struct StronglyConnectedComponents {
private:
    vector<vector<int>> g, rg;
    vector<int> order; 
    vector<bool> visited;
    int V;

    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (int u : g[v]) dfs(u);
        order.push_back(v);
    }

    void rdfs(int v, int c) {
        if (comp[v] != -1) return;
        comp[v] = c;
        for (int u : rg[v]) rdfs(u, c);
    }

public:
    vector<vector<int>> Graph; 
    vector<int> comp; 

    StronglyConnectedComponents(int v) : V(v) {
        g.resize(v);
        rg.resize(v);
        visited.resize(v);
        comp.resize(v);
    }
    
    void add_vertex(int n = 1) {
        V += n;
        g.resize(V);
        rg.resize(V);
        visited.resize(V);
        comp.resize(V);
    }

    void add_edge(int from, int to) {
        g[from].push_back(to);
        rg[to].push_back(from);
    }

    void build() {
        Graph = vector<vector<int>>();
        order = vector<int>();
        visited.assign(V, false);
        comp.assign(V, -1);
        for (int i = 0; i < V; i++) dfs(i);
        reverse(order.begin(), order.end());
        int number = 0;
        for (int i = 0; i < V; i++) {
            if (comp[order[i]] == -1) {
                rdfs(order[i], number++);
            }
        }
        Graph.resize(number);
        for (int i = 0; i < V; i++) {
            for (int v : g[i]) {
                if (comp[i] == comp[v]) continue;
                Graph[comp[i]].push_back(comp[v]);
            }
        }
    }
};

int main() {
    int n;cin >> n;
    vector<int> x(n);
    vector<int> a(n);
    StronglyConnectedComponents scc(n);
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        auto t = lower_bound(x.begin(), x.end(), x[i]-a[i]);
        int k = t-x.begin();
        if (k < n && x[k] == x[i]-a[i]) {
            scc.add_edge(i,k);
            cerr << i << "->" << k << endl;
        }
        t = lower_bound(x.begin(), x.end(), x[i]+a[i]);
        k = t-x.begin();
        if (k < n && x[k] == x[i]+a[i]) {
            scc.add_edge(i,k);
            cerr << i << "->" << k << endl;
        }
    }
    scc.build();
    vector<int> dp(scc.Graph.size());
    for (int i = 0; i < n; i++) {
        dp[scc.comp[i]] = max(x[i]+a[i],dp[scc.comp[i]]);
    }
    for (int i = scc.Graph.size()-1; i >= 0; i--) {
        for (int u : scc.Graph[i]) {
            dp[i] = max(dp[i],dp[u]);
        }
    }
    for (int i = 0; i < n; i++) {
        cout << dp[scc.comp[i]] - x[i] << endl;
    }
    return 0;
}
0