이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef BE_DEBUG
#define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
int n,h;
int a[200005];
int dp[10005];
int tur[10005];
int level,zereg[30];
vector<int> v[30];
int l,r;
int mn;
void build_tree(){
zereg[0] = 1;
for(int i = 0; i <= 30; i++){
if(zereg[i] >= 16){
level = i;
break;
}
zereg[i+1] = zereg[i] * 2;
}
for(int i=0; i<=level; i++){
for(int j=0; j<zereg[i]; j++){
v[i].pb(1e9);
}
}
}
void tree(){
for(int i=0; i<=5000; i++){
v[level][i] = dp[i];
}
for(int i=level-1; i>=0; i--){
for(int j=0; j<zereg[i]; j++){
v[i][j]=min(v[i+1][j*2],v[i+1][j*2+1]);
}
}
/*
for(int i=0; i<=level; i++){
for(int j=0; j<v[i].size(); j++){
cout<<v[i][j]<<" ";
}
cout<<"\n";
}
cout<<'\n';
*/
}
void dfs(int k, int x){
int y=zereg[level-k]*x;
int z=zereg[level-k]*(x+1)-1;
if(l<=y && z<=r){
mn = min(mn,v[k][x]);
return;
}
if(z<l || y>r || k==level) return;
dfs(k+1,x*2);
dfs(k+1,x*2+1);
}
void copy(){
for(int i=0; i<=5000; i++){
tur[i] = dp[i];
dp[i] = 1e9;
}
}
int main() {
IOS
cin >> n >> h;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
build_tree();
for(int i = 0; i <= 8192; i++){
dp[i] = abs(i - a[1]);
}
for(int i = 2; i <= n; i++){
tree();
copy();
for(int j = 0; j <= 5000; j++){
// (j-h) -> (j+h)
l = max(j-h,0);
r = min(j+h,5000);
mn=1e9;
dfs(0,0);
dp[j] = min(dp[j],mn+abs(a[i]-j));
}
}
int ans = 1e9;
for(int i=0; i<=5000; i++){
ans=min(ans,dp[i]);
}
cout << ans;
}
# | 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... |