提出 #54734937


ソースコード 拡げる

#include <iostream>
using namespace std;

#include <atcoder/segtree>
using namespace atcoder;

using P = pair<long long,long long>;

P op(P a,P b){
  return {max(a.first,b.first),min(a.second,b.second)};
}
P e(){
  return {-1e18,1e18};
}

// https://noshi91.hatenablog.com/entry/2023/02/18/005856
// T は辺コストの型
// cost(i,j) は頂点 i -> 頂点 j へのコスト(型 T)を返す関数
// 頂点番号は 0-ind
// 返り値は配列 dist で dist[i] が 0 -> i の最小コストに相当
// a[i][j] を j -> i を最後にするコスト dist[j]+cost(j,i) とする(j < i)
// a[i] が最小値を取るような j が欲しい これを amin で管理
template <typename T,typename F>
vector<T> monge_shortest_path(int n,T inf,const F & cost){
  vector<T> dist(n,inf);
  dist[0]=0;
  vector<int> amin(n,0);

  auto check=[&](int i,int k){
    // k -> i に行くもので暫定の最小値を更新できるかを確認
    if(k>=i)return;
    auto dist2=dist[k]+cost(k,i);
    if(dist2<dist[i]){
      dist[i]=dist2;
      amin[i]=k;
    }
  };

  auto subsolve=[&](auto& subsolve,int l,int r){
    // (l,r] について dist を計算する
    if(r-l==1)return;
    int m=(l+r)/2;
    for(int k=amin[l];k<=amin[r];k++)check(m,k);
    subsolve(subsolve,l,m);
    for(int k=l+1;k<=m;k++)check(r,k);
    subsolve(subsolve,m,r);
  };

  check(n-1,0);
  subsolve(subsolve,0,n-1);
  return dist;
}

int main(){
  int n;cin>>n;
  vector<long long> a(n);
  for(int i=0;i<n;i++)cin>>a[i];

  segtree<P,op,e> seg(n);
  for(int i=0;i<n;i++)seg.set(i,{a[i],a[i]});

  auto cost=[&](int i,int j){
    auto [mx,mn]=seg.prod(i,j);
    return mn-mx;
  };

  auto dist=monge_shortest_path<long long>(n+1,1e18,cost);
  cout<<-dist[n]<<endl;
}

提出情報

提出日時
問題 E - Good Partition
ユーザ Mitsubachi
言語 C++ 20 (gcc 12.2)
得点 600
コード長 1809 Byte
結果 AC
実行時間 168 ms
メモリ 16188 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 2
AC × 58
セット名 テストケース
Sample 00_sample_00, 00_sample_01
All 00_sample_00, 00_sample_01, 10_random_00, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 10_random_10, 10_random_11, 10_random_12, 10_random_13, 10_random_14, 10_random_15, 10_random_16, 10_random_17, 10_random_18, 10_random_19, 10_random_20, 10_random_21, 10_random_22, 10_random_23, 10_random_24, 10_random_25, 10_random_26, 10_random_27, 10_random_28, 10_random_29, 10_random_30, 10_random_31, 10_random_32, 10_random_33, 10_random_34, 10_random_35, 10_random_36, 10_random_37, 10_random_38, 10_random_39, 11_min_00, 12_max_00, 12_max_01, 12_max_02, 12_max_03, 12_max_04, 12_max_05, 12_max_06, 12_max_07, 12_max_08, 90_challenge_00, 90_challenge_01, 90_challenge_02, 91_challenge_00, 91_challenge_01, 91_challenge_02
ケース名 結果 実行時間 メモリ
00_sample_00 AC 1 ms 3516 KiB
00_sample_01 AC 1 ms 3664 KiB
10_random_00 AC 134 ms 15056 KiB
10_random_01 AC 159 ms 15776 KiB
10_random_02 AC 86 ms 9704 KiB
10_random_03 AC 145 ms 15616 KiB
10_random_04 AC 14 ms 4508 KiB
10_random_05 AC 120 ms 14800 KiB
10_random_06 AC 113 ms 14536 KiB
10_random_07 AC 25 ms 5988 KiB
10_random_08 AC 43 ms 6608 KiB
10_random_09 AC 93 ms 10036 KiB
10_random_10 AC 12 ms 3892 KiB
10_random_11 AC 55 ms 8896 KiB
10_random_12 AC 101 ms 10304 KiB
10_random_13 AC 32 ms 6308 KiB
10_random_14 AC 83 ms 9788 KiB
10_random_15 AC 89 ms 9952 KiB
10_random_16 AC 101 ms 10236 KiB
10_random_17 AC 5 ms 3896 KiB
10_random_18 AC 71 ms 9424 KiB
10_random_19 AC 6 ms 4004 KiB
10_random_20 AC 101 ms 10320 KiB
10_random_21 AC 124 ms 15040 KiB
10_random_22 AC 115 ms 14516 KiB
10_random_23 AC 60 ms 9000 KiB
10_random_24 AC 112 ms 14452 KiB
10_random_25 AC 143 ms 15304 KiB
10_random_26 AC 76 ms 9400 KiB
10_random_27 AC 120 ms 14804 KiB
10_random_28 AC 159 ms 15924 KiB
10_random_29 AC 151 ms 15544 KiB
10_random_30 AC 87 ms 9808 KiB
10_random_31 AC 21 ms 4948 KiB
10_random_32 AC 92 ms 9688 KiB
10_random_33 AC 69 ms 9252 KiB
10_random_34 AC 153 ms 15536 KiB
10_random_35 AC 121 ms 14772 KiB
10_random_36 AC 61 ms 8988 KiB
10_random_37 AC 137 ms 15228 KiB
10_random_38 AC 15 ms 4724 KiB
10_random_39 AC 49 ms 6616 KiB
11_min_00 AC 1 ms 3496 KiB
12_max_00 AC 168 ms 16112 KiB
12_max_01 AC 165 ms 16036 KiB
12_max_02 AC 166 ms 16116 KiB
12_max_03 AC 165 ms 16040 KiB
12_max_04 AC 165 ms 16096 KiB
12_max_05 AC 166 ms 16096 KiB
12_max_06 AC 166 ms 16188 KiB
12_max_07 AC 166 ms 16024 KiB
12_max_08 AC 165 ms 16060 KiB
90_challenge_00 AC 156 ms 16188 KiB
90_challenge_01 AC 156 ms 16080 KiB
90_challenge_02 AC 155 ms 16040 KiB
91_challenge_00 AC 156 ms 16120 KiB
91_challenge_01 AC 157 ms 16048 KiB
91_challenge_02 AC 157 ms 16044 KiB