結果

問題 No.1077 Noelちゃんと星々4
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-08-26 11:45:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,751 ms / 2,000 ms
コード長 1,964 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 93,940 KB
実行使用メモリ 263,680 KB
最終ジャッジ日時 2024-04-24 17:54:01
合計ジャッジ時間 19,079 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
5,248 KB
testcase_01 AC 19 ms
6,144 KB
testcase_02 AC 1,149 ms
181,632 KB
testcase_03 AC 1,496 ms
234,368 KB
testcase_04 AC 450 ms
72,704 KB
testcase_05 AC 358 ms
55,552 KB
testcase_06 AC 593 ms
93,696 KB
testcase_07 AC 696 ms
107,648 KB
testcase_08 AC 523 ms
78,976 KB
testcase_09 AC 446 ms
68,480 KB
testcase_10 AC 1,570 ms
234,624 KB
testcase_11 AC 1,015 ms
152,960 KB
testcase_12 AC 829 ms
126,336 KB
testcase_13 AC 332 ms
52,736 KB
testcase_14 AC 1,528 ms
229,376 KB
testcase_15 AC 801 ms
122,112 KB
testcase_16 AC 522 ms
81,024 KB
testcase_17 AC 912 ms
138,240 KB
testcase_18 AC 1,597 ms
238,848 KB
testcase_19 AC 312 ms
50,304 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 1,751 ms
263,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define M (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;

template <typename T>
struct SegmentTree{
    typedef function<T(T,T)> F;
    int n;
    F f;
    T unit;
    vector<T> dat;
    SegmentTree(){};
    SegmentTree(int newn,F f,T t):f(f),unit(t){
        init(newn);
    }
    SegmentTree(const vector<T> &v,F f,T t):f(f),unit(t){
        int n_ = v.size();
        init(n_);
        for(int i=0;i<n_;i++)dat[n+i]=v[i];
        for(int i=n-1;i;i--){
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
        }
    }
 
    void init(int n_){
        n=1;
        while(n<n_)n<<=1;
        dat.assign(n<<1,unit);
    }
    
    void update(int k,T x){
        dat[k+=n]=x;
        while(k>>=1)
            dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
    }
    T query(int a,int b){
        T vl=unit,vr=unit;
        for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1){
            if(l&1)vl=f(vl,dat[l++]);
            if(r&1)vr=f(dat[--r],vr);
        }
        return f(vl,vr);
    }
 
};

SegmentTree<ll> dp[1010];

int main(void){
    int n;
    cin>>n;
    vector<int>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    auto f = [](ll a,ll b){
        return min(a,b);
    };
    for(int i=0;i<=n;i++){
        if(i==0) {
            dp[i] = SegmentTree<ll>(10001,f,(ll)INF);
            dp[i].update(0,0);
        }
        else dp[i] = SegmentTree<ll>(10001,f,(ll)INF);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<10001;j++){
            ll A = dp[i].query(0,j+1);
            if(dp[i+1].query(j,j+1)>A+abs(a[i]-j)){
                dp[i+1].update(j,A+abs(a[i]-j));
            }
        }
    }
    ll res = (ll)INF;
    for(int i=0;i<10001;i++)res = min(dp[n].query(i,i+1),res);
    cout<<res<<endl;
}

0