結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー
提出日時 2022-10-14 22:18:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 4,061 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 124,636 KB
実行使用メモリ 12,904 KB
最終ジャッジ日時 2023-09-08 22:12:10
合計ジャッジ時間 8,584 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 119 ms
9,800 KB
testcase_05 AC 99 ms
7,992 KB
testcase_06 AC 19 ms
4,380 KB
testcase_07 AC 23 ms
4,956 KB
testcase_08 AC 121 ms
11,936 KB
testcase_09 AC 97 ms
7,244 KB
testcase_10 AC 50 ms
4,624 KB
testcase_11 AC 91 ms
8,524 KB
testcase_12 AC 69 ms
6,220 KB
testcase_13 AC 74 ms
9,632 KB
testcase_14 AC 60 ms
5,268 KB
testcase_15 AC 80 ms
9,324 KB
testcase_16 AC 64 ms
5,460 KB
testcase_17 AC 48 ms
8,532 KB
testcase_18 AC 75 ms
9,588 KB
testcase_19 AC 148 ms
11,528 KB
testcase_20 AC 32 ms
5,160 KB
testcase_21 AC 66 ms
6,480 KB
testcase_22 AC 62 ms
5,268 KB
testcase_23 AC 116 ms
10,052 KB
testcase_24 AC 163 ms
12,744 KB
testcase_25 AC 160 ms
12,848 KB
testcase_26 AC 162 ms
12,856 KB
testcase_27 AC 163 ms
12,824 KB
testcase_28 AC 161 ms
12,716 KB
testcase_29 AC 160 ms
12,736 KB
testcase_30 AC 158 ms
12,820 KB
testcase_31 AC 162 ms
12,856 KB
testcase_32 AC 157 ms
12,904 KB
testcase_33 AC 165 ms
12,708 KB
testcase_34 AC 85 ms
5,984 KB
testcase_35 AC 76 ms
5,668 KB
testcase_36 AC 75 ms
5,332 KB
testcase_37 AC 70 ms
5,452 KB
testcase_38 AC 81 ms
5,484 KB
testcase_39 AC 63 ms
5,648 KB
testcase_40 AC 64 ms
5,752 KB
testcase_41 AC 63 ms
5,772 KB
testcase_42 AC 63 ms
5,296 KB
testcase_43 AC 63 ms
5,820 KB
testcase_44 AC 97 ms
12,736 KB
testcase_45 AC 96 ms
12,668 KB
testcase_46 AC 94 ms
12,180 KB
testcase_47 AC 94 ms
12,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cmath>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cassert>
using namespace std;
namespace atcoder{};using namespace atcoder;

using ull = uint64_t;
using ll = int64_t;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
using ld = long double;
using P = pair<ll,ll>;
using G = vector<vector<int>>;

#ifdef MYDEBUG
template<class C> string join(const C& c){stringstream ss; for(auto it=c.begin(); it!=c.end();it++){ss << *it;if(it!=c.end())ss<<", ";}return ss.str();}
template<class C> ostream &operator<<(ostream &os, const vector<C>& c){for(auto it=c.begin(); it!=c.end();it++){os << *it;if(it!=c.end())os<<", ";}return os;}
#define LO(...) fprintf(stderr, __VA_ARGS__)
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl
#else
#define LO(...) (void)0
#define debug(x) (void)0
#endif

#define reps(i,a,n) for(ll i##_len = (ll)(n), i = (a); i < i##_len; ++i)
#define rep(i,n) reps(i,0,n)
#define rrep(i,n) reps(i,1,n+1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)

#define inp(i) ll i; cin >> i;
#define inps(s) string s; cin >> s;
#define inpp(p) cin >> (p).first >> (p).second
#define inpv(v,N) vll v(N);rep(i,N)cin>>v[i];
#define inpg(g,N,M) g.resize(N);rep(i,M){inp(a);inp(b);a--,b--;g[a].push_back(b);g[b].push_back(a);}
#define all(v) begin(v),end(v)
#define YESNO(b) cout<<(b?"YES\n":"NO\n")
#define yesno(b) cout<<(b?"yes\n":"no\n")
#define YesNo(b) cout<<(b?"Yes\n":"No\n")
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"

#define SP cout << " "
#define ENDL cout << "\n"
#define ou(i) cout << (i)
#define ous(i) cout << (i) << " "
#define oul(i) cout << (i) << "\n"
#define setfp() cout << fixed << setprecision(16)

template<typename C> void ouv(const C &v){for(auto &&e:v){cout << e;if(&e != &v.back()) cout << ' ';}cout << "\n";}
template<typename C> void ouvadd(const C &v){for(auto &&e:v){cout << e+1;if(&e != &v.back()) cout << ' ';}cout << "\n";}

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

inline ll lg(ll __n) { return sizeof(ll) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }

int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,1,-1,-1};

constexpr ll INF = 1e18;

ll dpa[101000][2];
ll dpb[101000][2];

int main(){
    inp(N);
    inp(M);
    inpv(H,N);
    G g;
    inpg(g,N,M);

    rep(i,N){
        dpa[i][0] = -INF;
        dpa[i][1] = -INF;
        dpb[i][0] = -INF;
        dpb[i][1] = -INF;
    }
    dpa[0][0] = 0;
    rep(i,N-1){
        ll mx0 = -INF;
        ll mx1 = -INF;
        for(int d : g[i+1]){
            if(d > i+1)continue;
            LO("%ld <> %ld\n", i+1,d);
            if(H[i+1] > H[d]){
                chmax(mx1, dpa[d][0] + H[i+1] - H[d]);
            }else{
                chmax(mx0, dpa[d][0]);
                chmax(mx0, dpa[d][1]);
            }
        }
        dpa[i+1][0] = mx0;
        dpa[i+1][1] = mx1;
        LO("%ld -> %ld %ld\n",i, dpa[i+1][0], dpa[i+1][1]);
    }
    ll an = max(dpa[N-1][0], dpa[N-1][1]);
    if(an < -1e16){
        oul(-1);
    }else{
        oul(an);
    }

    dpb[N-1][0] = 0;
    for(ll i = N-2; i>=0; i--){
        ll mx0 = -INF;
        ll mx1 = -INF;
        for(int d : g[i]){
            if(d < i)continue;
            if(H[i] > H[d]){
                chmax(mx1, dpb[d][0] + H[i] - H[d]);
            }else{
                chmax(mx0, dpb[d][0]);
                chmax(mx0, dpb[d][1]);
            }
        }
        dpb[i][0] = mx0;
        dpb[i][1] = mx1;
    }
    an = max(dpb[0][0], dpb[0][1]);
    if(an < -1e16){
        oul(-1);
    }else{
        oul(an);
    }
    return 0;
}
0