Submission #645790

# Submission time Handle Problem Language Result Execution time Memory
645790 2022-09-28T02:13:09 Z kith14 Safety (NOI18_safety) C++17
0 / 100
2 ms 1024 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>

#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back

const ll N = 5e3+5, MOD = 1e9+7;
ll tc = 1, n, m, ar[N], br[N], dp[2][N];
string s, s1, s2, ye = "YA", no = "TIDAK";

ll tree[2][N*4];

void build(ll l, ll r, ll idx, ll bt){
  if (l == r){
    tree[bt][idx] = dp[bt][l];
    return;
  }
  ll md = (l+r)/2;
  build(l,md,idx*2,bt);
  build(md+1,r,idx*2+1,bt);
  tree[bt][idx] = min(tree[bt][idx*2],tree[bt][idx*2+1]); 
}

ll que(ll l, ll r, ll idx, ll x, ll y, ll bt){
  if ( l > r || l > y || r < x) return 1e18;
  if (x <= l && r <= y) return tree[bt][idx];
  ll md = (l+r)/2;
  return min(que(l,md,idx*2,x,y,bt),que(md+1,r,idx*2+1,x,y,bt));
}

void input(){
  cin >> n >> m;
  repp(i,1,n) cin >> ar[i];
}

void solve(){
  ll lm = 5000;
  repp(i,0,lm){
    dp[n%2][i] = abs(i-ar[n]);
  }

  repm(i,n-1,1){
    ll cur = (i)%2, pre = (i+1)%2;
    deque<pairll> dq;
    repp(j,0,m){
      while(dq.size() && dp[pre][j] < dq.back().fr){
        dq.pop_back();
      }
      dq.push_back(mp(dp[pre][j],j));
    } 
    repp(j,0,lm){
      ll btl = max(0LL,j-m), btr = min(j+m,lm);
      while(dq.size() && dq.front().sc < btl) dq.pop_front();
      while(dq.size() && dp[pre][btr] < dq.back().fr){
        dq.pop_back();
      }
      dq.push_back(mp(dp[pre][btr],j));
      dp[cur][j] = dq.front().fr+abs(ar[i]-j);
    }
  }

  ll ans = 1e18;
  repp(i,0,lm){
    ans = min(ans,dp[1][i]);
  }
  // repp(i,0,10){
  //   cout << i << " " << dp[1][i] << endl;
  // }
  cout << ans << endl;
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  //cin >> tc;
  while(tc--){
    input();
    solve();
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Runtime error 2 ms 1024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Runtime error 2 ms 1024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Runtime error 2 ms 1024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Runtime error 2 ms 1024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Runtime error 2 ms 1024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Runtime error 2 ms 1024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -