結果

問題 No.1077 Noelちゃんと星々4
ユーザー outlineoutline
提出日時 2020-06-12 22:38:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,428 ms / 2,000 ms
コード長 2,621 bytes
コンパイル時間 1,234 ms
コンパイル使用メモリ 116,300 KB
実行使用メモリ 135,304 KB
最終ジャッジ日時 2023-09-06 10:47:29
合計ジャッジ時間 15,757 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 977 ms
93,844 KB
testcase_03 AC 1,264 ms
120,600 KB
testcase_04 AC 380 ms
38,428 KB
testcase_05 AC 279 ms
29,664 KB
testcase_06 AC 491 ms
49,012 KB
testcase_07 AC 569 ms
56,100 KB
testcase_08 AC 414 ms
41,612 KB
testcase_09 AC 356 ms
36,228 KB
testcase_10 AC 1,264 ms
120,772 KB
testcase_11 AC 820 ms
79,056 KB
testcase_12 AC 670 ms
65,620 KB
testcase_13 AC 272 ms
28,336 KB
testcase_14 AC 1,235 ms
118,012 KB
testcase_15 AC 645 ms
63,500 KB
testcase_16 AC 425 ms
42,636 KB
testcase_17 AC 737 ms
71,744 KB
testcase_18 AC 1,294 ms
122,968 KB
testcase_19 AC 257 ms
27,020 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1,428 ms
135,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template<typename Monoid>
struct SegmentTree{
    using F = function<Monoid(Monoid, Monoid)>;

    int sz;
    vector<Monoid> seg;

    const F f;      // モノイドに対して二項演算を行う関数オブジェクト
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1){
        sz = 1;
        while(sz < n)   sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x){
        seg[k + sz] = x;
    }

    void build(){
        for(int k = sz - 1; k > 0; --k){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    void update(int k, const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k >>= 1){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    Monoid query(int a, int b){
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1){
            if(a & 1)   L = f(L, seg[a++]);
            if(b & 1)   R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const{
        return seg[k + sz];
    }
};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int> y(n);
    int maxe = 0;
    for(int i = 0; i < n; ++i){
        cin >> y[i];
        chmax(maxe, y[i]);
    }

    vector<SegmentTree<int>> dp(n + 1, SegmentTree<int>(maxe + 1, [](int a, int b){return min(a, b);}, INF));
    dp[0].update(0, 0);
    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= maxe; ++j){
            int val = dp[i].query(0, j + 1) + abs(y[i] - j);
            if(dp[i + 1][j] > val){
                dp[i + 1].update(j, val);
            }
        }
    }
    cout << dp[n].query(0, maxe + 1) << endl;
}
0