結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー momoyuumomoyuu
提出日時 2022-10-14 23:55:39
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 2,848 bytes
コンパイル時間 4,088 ms
コンパイル使用メモリ 254,800 KB
実行使用メモリ 17,296 KB
最終ジャッジ日時 2023-09-09 00:54:43
合計ジャッジ時間 10,512 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 147 ms
13,268 KB
testcase_05 AC 115 ms
10,872 KB
testcase_06 AC 20 ms
4,856 KB
testcase_07 AC 26 ms
5,348 KB
testcase_08 AC 143 ms
15,232 KB
testcase_09 AC 114 ms
10,088 KB
testcase_10 AC 55 ms
6,908 KB
testcase_11 AC 105 ms
11,236 KB
testcase_12 AC 77 ms
8,160 KB
testcase_13 AC 83 ms
11,764 KB
testcase_14 AC 66 ms
7,412 KB
testcase_15 AC 90 ms
11,664 KB
testcase_16 AC 74 ms
7,788 KB
testcase_17 AC 55 ms
10,012 KB
testcase_18 AC 87 ms
12,064 KB
testcase_19 AC 171 ms
15,500 KB
testcase_20 AC 35 ms
5,988 KB
testcase_21 AC 74 ms
8,572 KB
testcase_22 AC 70 ms
7,752 KB
testcase_23 AC 128 ms
13,512 KB
testcase_24 AC 190 ms
17,180 KB
testcase_25 AC 188 ms
17,148 KB
testcase_26 AC 189 ms
17,108 KB
testcase_27 AC 196 ms
17,196 KB
testcase_28 AC 188 ms
17,136 KB
testcase_29 AC 189 ms
17,256 KB
testcase_30 AC 190 ms
17,288 KB
testcase_31 AC 188 ms
17,296 KB
testcase_32 AC 187 ms
17,208 KB
testcase_33 AC 186 ms
17,212 KB
testcase_34 AC 97 ms
9,828 KB
testcase_35 AC 84 ms
9,332 KB
testcase_36 AC 91 ms
8,872 KB
testcase_37 AC 78 ms
9,420 KB
testcase_38 AC 90 ms
8,860 KB
testcase_39 AC 70 ms
9,952 KB
testcase_40 AC 71 ms
9,948 KB
testcase_41 AC 70 ms
9,552 KB
testcase_42 AC 70 ms
10,084 KB
testcase_43 AC 73 ms
9,716 KB
testcase_44 AC 102 ms
14,588 KB
testcase_45 AC 103 ms
14,592 KB
testcase_46 AC 99 ms
14,772 KB
testcase_47 AC 101 ms
14,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<class t> using vc = vector<t>;
template<class t> using vvc = vc<vc<t>>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vc<int>;
using vvi = vvc<int>;
using vl = vc<ll>;
using vvl = vvc<ll>;

#define rep(i,a,b) for (int i = (int)(a); i < (int)(b); i++)
#define irep(i,a,b) for (int i = (int)(a); i > (int)(b); i--)
#define all(a) a.begin(),a.end()
#define print(n) cout << n << '\n'
#define pritn(n) print(n)
#define printv(n,a) {copy(all(n),ostream_iterator<a>(cout," ")); cout<<"\n";}
#define printvv(n,a) {for(auto itr:n) printv(itr,a);}
#define rup(a,b) (a+b-1)/b
#define input(A,N) rep(i,0,N) cin>>A[i]
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)

template< typename T >
struct edge{
    int from,to;
    T cost;
    /*
    *to:繋がってる先
    *cost:辺のコスト
    */
    edge(int from,int to, T cost):from(from),to(to),cost(cost){};
    edge &operator=(const int& x){
        to = x;
        return *this;
    }
};

template< typename T >
struct graph{
    int n;
    vector<vector<edge<T>>> e;
    graph(int n):n(n),e(n){}

    void addedge(int from,int to,T cost){
        e[from].emplace_back(from,to,cost);
    }

    vector<edge<T>> &operator[](int i) {
        return e[i];
    }
};


int main(){
    cout << fixed << setprecision(15);
    
    int n,m;
    cin>>n>>m;
    vl h(n);
    input(h,n);
    graph<int> g(n);
    rep(i,0,m){
        int u,v;
        cin>>u>>v;
        u--;v--;
        g.addedge(u,v,1);
        g.addedge(v,u,1);
    }
    vvc<ll> dp(n,vc<ll>(2,-1));
    dp[0][0] = 0;
    dp[0][1] = 0;
    for(int i = 0;i<n-1;i++){
        for(auto e:g[i]){
            if(i>=e.to) continue;
            int ni = e.to;
            ll d = h[ni] - h[i];
            if(d<0){
                if(dp[i][0]!=-1) chmax(dp[ni][1],dp[i][0]);
                if(dp[i][1]!=-1) chmax(dp[ni][1],dp[i][1]);
            }else{
                if(dp[i][1] == -1) continue;
                chmax(dp[ni][0],dp[i][1]+d);
            }
        }
    }
    ll a = max(dp[n-1][0],dp[n-1][1]);
    for(int i = 0;i<n;i++){
        for(int j = 0;j<2;j++) dp[i][j] = -1;
    }
    dp[n-1][0] = 0;
    dp[n-1][1] = 0;
    for(int i = n-1;i>=1;i--){
        for(auto e:g[i]){
            if(i<=e.to) continue;
            int ni = e.to;
            ll d = h[ni] - h[i];
            if(d<0){
                if(dp[i][0]!=-1) chmax(dp[ni][1],dp[i][0]);
                if(dp[i][1]!=-1) chmax(dp[ni][1],dp[i][1]);
            }else{
                if(dp[i][1] == -1) continue;
                chmax(dp[ni][0],dp[i][1]+d);
            }
        }
    }
    ll b = max(dp[0][0],dp[0][1]);
    //printvv(dp,ll);
    print(a);
    print(b);
    //system("pause");
    return 0;
}
0