結果

問題 No.2157 崖
ユーザー milanis48663220milanis48663220
提出日時 2022-12-09 21:46:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,193 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 128,136 KB
実行使用メモリ 35,584 KB
最終ジャッジ日時 2024-04-22 21:36:10
合計ジャッジ時間 8,875 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
5,376 KB
testcase_13 WA -
testcase_14 AC 593 ms
32,768 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 502 ms
28,288 KB
testcase_18 AC 489 ms
27,648 KB
testcase_19 WA -
testcase_20 AC 648 ms
35,584 KB
testcase_21 AC 649 ms
35,456 KB
testcase_22 AC 467 ms
26,624 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/segtree>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll inf = 1e18;

ll op(ll x, ll y){
    return min(x, y);
}

ll e(){
    return inf;
}

using Seg = atcoder::segtree<ll, op, e>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    auto d = vec2d(n, m, 0ll);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> d[i][j];
        }
        sort(d[i].begin(), d[i].end());
    }
    auto dp = vec2d(n, m, inf);
    for(int j = 0; j < m; j++) dp[0][j] = 0;
    for(int i = 1; i < n; i++){
        Seg seg(m);
        int c = 0;
        for(int j = 0; j < m; j++){
            while(c < m && d[i-1][c] <= d[i][j]){
                seg.set(c, dp[i-1][c]-d[i-1][c]);
                c++;
            }
            dp[i][j] = min(seg.prod(0, c)+d[i][j], seg.prod(c, m)-d[i][j]);
        }
    }
    ll ans = *min_element(dp[n-1].begin(), dp[n-1].end());
    if(ans < inf/2) cout << ans << endl;
    else cout << -1 << endl;
}
0