# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1124377 | LTTrungCHL | Safety (NOI18_safety) | C++20 | 223 ms | 99212 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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |