Submission #714812

# Submission time Handle Problem Language Result Execution time Memory
714812 2023-03-25T09:55:02 Z murad_2005 Stranded Far From Home (BOI22_island) C++14
0 / 100
139 ms 19312 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define pllll pair<ll, ll>
#define size(v) v.size()
#define all(v) v.begin(), v.end()
#define INF 2e9
#define f first
#define s second

using namespace std;

vector<vector<int>>g;
vector<ll>Sum;
vector<ll>s;

void dfs(int node, int par){
    for(int to : g[node]){
        if(to != par){
            dfs(to, node);
            Sum[node] += Sum[to];
        }
    }
    Sum[node] += s[node];
}

void DFS(int node, int par, vector<int>&isOkay){
    for(int to : g[node]){
        if(to != par){
            if(!isOkay[node]){
                isOkay[to] = 0;
            }else{
                if(Sum[to] >= s[node]){
                    isOkay[to] = 1;
                }
            }
            DFS(to, node, isOkay);
        }
    }
}

void Sub2(int n, int m, vector<ll>&Sum){
    vector<int>isOkay(n + 1, 0);
    isOkay[1] = 1;
    dfs(1, 1);
    DFS(1, 1, isOkay);
    for(int i = 1; i <= n; ++i){
        cout << isOkay[i];
    }
    cout << "\n";

}

void Sub3(int n, int m){
    vector<ll>pref(n + 1, 0);
    for(int i = 1; i <= n; ++i){
        pref[i] = pref[i - 1] + s[i];
    }
    vector<int>Left(n + 1, 0), Right(n + 1, 0);
    ll r = 0;
    for(int i = 2; i <= n; ++i){ // left
        if(s[i - 1] > s[i]){
            if(Left[i - 1] > 0){
                Left[i] = Left[i - 1];
            }else{
                Left[i] = i - 1;
            }
            r += (s[i - 1] - s[i]);
        }else{
            if(r > s[i]){
                Left[i] = Left[i - 1];
            }
            r = max(0ll, r - s[i]);
        }
    }
    r = 0;
    for(int  i = n - 1; i >= 1; i--){// right
        if(s[i + 1] > s[i]){
            if(Right[i + 1] > 0){
                Right[i] = Right[i + 1];
            }else{
                Right[i] = i + 1;
            }
            r += (s[i + 1] - s[i]);
        }else{
            if(r > s[i]){
                Right[i] = Right[i + 1];
            }
            r = max(0ll, r - s[i]);
        }
    }
    vector<ll>res(n + 1);
    for(int i = 1; i <= n; ++i){
        if(Left[i] + Right[i] == 0){
            res[i] = 1;
        }else if(Left[i] > 0 && Right[i] > 0){
            res[i] = 0;
        }else{
            if(Left[i] > 0){
                ll x = Left[i];
                if(pref[n] - pref[x] >= s[x]){
                    res[i] = 1;
                }else{
                    res[i] = 0;
                }
            }else{
                ll x = Right[i];
                if(pref[x - 1] >= s[x]){
                    res[i] = 1;
                }else{
                    res[i] = 0;
                }
            }
        }
    }
    for(int i = 1; i <= n; i++){
        cout << res[i];
    }
    cout << "\n";
}

void solve(){
    int n, m;
    cin >> n >> m;
    s.resize(n + 1);
    g.resize(n + 1);
    Sum.assign(n + 1, 0);
    for(int i = 1; i <= n; ++i){
        cin >> s[i];
    }
    for(int i = 1; i <= m; ++i){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    //Sub2(n, m, Sum);
    Sub3(n, m);



}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    // cin >> t;
    t = 1;
    while(t--){
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 468 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 115 ms 19312 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 135 ms 19104 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 139 ms 19192 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 468 KB Output isn't correct
5 Halted 0 ms 0 KB -