Submission #645799

# Submission time Handle Problem Language Result Execution time Memory
645799 2022-09-28T02:59:05 Z kith14 Safety (NOI18_safety) C++17
40 / 100
216 ms 5252 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 = 2e5+5, MOD = 1e9+7;
ll tc = 1, n, m, ar[N], br[N], dp[2][N];
string s, s1, s2, ye = "YA", no = "TIDAK";

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

void subm0(){
  sort(ar+1,ar+n+1);
  repp(i,1,n){
    br[i] = br[i-1]+ar[i];
  }
  ll ans = 1e18;
  repp(i,1,n){
    ll tt;
    tt = ar[i]*i-br[i];
    tt += br[n]-br[i]-(n-i)*ar[i];
    //cout << i << " " << tt << endl;
    ans = min(ans,tt);
  }
  cout << ans << endl;
  exit(0);
}

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

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

  ll ans = 1e18;
  repp(i,0,lm){
    ans = min(ans,dp[1][i]);
  }
  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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# 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 Correct 29 ms 3404 KB Output is correct
2 Correct 34 ms 4988 KB Output is correct
3 Correct 34 ms 5252 KB Output is correct
# 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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 23 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 19 ms 464 KB Output is correct
16 Correct 22 ms 420 KB Output is correct
17 Correct 20 ms 428 KB Output is correct
18 Correct 17 ms 340 KB Output is correct
19 Correct 22 ms 420 KB Output is correct
# 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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 23 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 19 ms 464 KB Output is correct
16 Correct 22 ms 420 KB Output is correct
17 Correct 20 ms 428 KB Output is correct
18 Correct 17 ms 340 KB Output is correct
19 Correct 22 ms 420 KB Output is correct
20 Correct 18 ms 340 KB Output is correct
21 Correct 21 ms 436 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 15 ms 476 KB Output is correct
24 Correct 21 ms 452 KB Output is correct
25 Correct 18 ms 452 KB Output is correct
26 Correct 35 ms 344 KB Output is correct
27 Correct 20 ms 340 KB Output is correct
# 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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 23 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 19 ms 464 KB Output is correct
16 Correct 22 ms 420 KB Output is correct
17 Correct 20 ms 428 KB Output is correct
18 Correct 17 ms 340 KB Output is correct
19 Correct 22 ms 420 KB Output is correct
20 Correct 18 ms 340 KB Output is correct
21 Correct 21 ms 436 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 15 ms 476 KB Output is correct
24 Correct 21 ms 452 KB Output is correct
25 Correct 18 ms 452 KB Output is correct
26 Correct 35 ms 344 KB Output is correct
27 Correct 20 ms 340 KB Output is correct
28 Correct 213 ms 444 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 215 ms 472 KB Output is correct
31 Correct 169 ms 468 KB Output is correct
32 Correct 174 ms 440 KB Output is correct
33 Correct 216 ms 468 KB Output is correct
34 Correct 202 ms 484 KB Output is correct
# 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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 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 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -