Submission #1124377

#TimeUsernameProblemLanguageResultExecution timeMemory
1124377LTTrungCHLSafety (NOI18_safety)C++20
40 / 100
223 ms99212 KiB
///***LTT***///
/// ->NHAT QUOC GIA<- ///
#include<bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
using namespace std;
vector <int> lg2;
//void MAKE_LOG_ARR(int n){
//    lg2.resize(n + 3);
//    for (int i = 1;i <= n;++i){
//        lg2[i] = __lg(i);
//    }
//}
const long long oo = 1e9+7;
const int N = 2 * 1e5 + 10;
int n, h;
int a[N];
int dp[5050][5050];
deque <int> dq;
int mxa = 0;
int mi[5050];
namespace sub4{
    void solve(){
        for (int i = 1;i <= n;++i){
            for (int j = 0;j <= mxa;++j){
                dp[i][j] = abs(a[i] - j) + mi[j];
            }
            while (!dq.empty()) dq.pop_back();
            for (int j = 0;j <= h;++j){
                while (!dq.empty() and dp[i][dq.back()] >= dp[i][j]) dq.pop_back();
                dq.push_back(j);
            }
            for (int j = 0;j <= mxa;++j){
                while (j + h <= mxa and !dq.empty() and dp[i][dq.back()] >= dp[i][j + h]) dq.pop_back();
                if (j + h <= mxa)
                dq.push_back(j + h);
                while (!dq.empty() and dq.front() < j - h) dq.pop_front();
                mi[j] = dp[i][dq.front()];
            }
        }
        int ans = oo;
        for (int i = 0;i <= mxa;++i){
            ans = min(ans, dp[n][i]);
        }
        cout << ans;
        return;
    }
};
namespace sub1{
    long long f(int x){
        long long res = 0;
        for (int i = 1;i <= n;++i){
            res += abs(a[i] - x);
        }
        return res;
    }
    void solve(){
        int l = 0, r = mxa;
        while (r - l > 1){
            int mid = (l + r)/2;
            if (f(mid) > f(mid + 1)){
                l = mid;
            } else r = mid;
        }
        cout << f(l + 1);
        return;
    }
};
void solve(){
    cin >> n >> h;
    for (int i = 1;i <= n;++i){
        cin >> a[i];
        mxa = max(mxa, a[i]);
    }
    bool ok = true;
    for (int i = 2;i <= n;++i){
        if (abs(a[i] - a[i - 1]) > h){
            ok = false;
            break;
        }
    }
    if (ok){
        cout << 0;
        return;
    }
    if (h == 0){
        sub1::solve();
        return;
    }
    if (n <= 5000 and mxa <= 5000){
        sub4::solve();
        return;
    }
    return;
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    if (fopen("ltt.inp", "r")){
        freopen("ltt.inp", "r", stdin);
        freopen("ltt.out", "w", stdout);
    }
//    int t;
//    cin >> t;
//    while(t--){
    solve();
//    }
    return 0;
}






////

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen("ltt.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen("ltt.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...