Submission #1095326

# Submission time Handle Problem Language Result Execution time Memory
1095326 2024-10-01T21:11:56 Z epicci23 Text editor (CEOI24_editor) C++17
100 / 100
827 ms 464724 KB
#include "bits/stdc++.h"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;

const int INF = 1e18;

void _(){
  int n,a,b,c,d;
  cin >> n >> a >> b >> c >> d;
  vector<int> pre(n+5),suf(n+5),ar(n+5);
  int Sparse[n+5][20];
  for(int i=1;i<=n;i++){
  	cin >> ar[i];
  	ar[i]++;
    Sparse[i][0]=ar[i];
  }
  for(int j=1;j<20;j++){
   for(int i=1;i+(1<<j)-1<=n;i++){
    Sparse[i][j]=min(Sparse[i][j-1],Sparse[i+(1<<(j-1))][j-1]);
   }
  }
  
  auto Get=[&](int l,int r)->int {
    if(l>r) swap(l,r);
    int k=__lg(r-l+1);
    return min(Sparse[l][k],Sparse[r-(1<<k)+1][k]);
  };

  pre[a]=suf[a]=b;
  
  for(int i=a-1;i>=1;i--) pre[i]=min(pre[i+1],ar[i]);
  for(int i=a+1;i<=n;i++) suf[i]=min(suf[i-1],ar[i]);

  int ans=INF;
  
  if(a<=c){
    for(int i=a;i>=1;i--){
      ans=min(ans,a-i+c-i+abs(min(pre[i],suf[c])-d));
    }
  } 
  else{
    for(int i=a;i<=n;i++){
      ans=min(ans,i-a+i-c+abs(min(suf[i],pre[c])-d));
    }
  }


  priority_queue<array<int,2>> pq;
  vector<int> dist(2*n+5,INF),vis(2*n+5,0);
  vector<array<int,2>> v[2*n+5];
  int st=2*n+1,en=2*n+2;

  v[st].push_back({2*a-1,b-1});
  v[st].push_back({2*a,ar[a]-b});
  v[2*a-1].push_back({st,b-1});
  v[2*a].push_back({st,ar[a]-b});

  v[en].push_back({2*c-1,d-1});
  v[en].push_back({2*c,ar[c]-d});
  v[2*c-1].push_back({en,d-1});
  v[2*c].push_back({en,ar[c]-d});

  for(int i=1;i<=n;i++){
    v[2*i].push_back({2*i-1,ar[i]-1});
    v[2*i-1].push_back({2*i,ar[i]-1});
    if(i<n){
      v[2*i].push_back({2*i+1,1});
      v[2*i+1].push_back({2*i,1});
      v[2*i-1].push_back({2*i+1,1});
      v[2*i+1].push_back({2*i-1,1});
      if(ar[i]<=ar[i+1]){
        v[2*i].push_back({2*i+2,1+ar[i+1]-ar[i]});
        v[2*i+2].push_back({2*i,1});
      }
      else{
        v[2*i].push_back({2*i+2,1});
        v[2*i+2].push_back({2*i,1+ar[i]-ar[i+1]});
      }
    }
  }

  for(int i=1;i<=n;i++){
    if(Get(a,i)<=b && ar[i]==Get(a,i)) v[st].push_back({2*i,abs(a-i)});
  }

  dist[st]=0;
  pq.push({0,st});

  while(!pq.empty()){
    int c=pq.top()[1];
    pq.pop();
    if(vis[c]) continue;
    vis[c]=1;
    for(auto [u,d]:v[c]){
      if(!vis[u] && d+dist[c]<dist[u]){
        dist[u]=d+dist[c];
        pq.push({-dist[u],u});
      }
    }
  }

  ans=min(ans,dist[en]);
  for(int i=1;i<=n;i++){
    if(Get(i,c)==ar[i]) ans=min(ans,dist[2*i]+abs(c-i)+abs(d-ar[i]));
  }

  cout << ans << '\n';
}

int32_t main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tc=1;//cin >> tc;
  while(tc--) _();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 672 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 856 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 1272 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 672 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 1272 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 600 KB Output is correct
36 Correct 1 ms 856 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 1 ms 860 KB Output is correct
39 Correct 1 ms 856 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 856 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 479 ms 373292 KB Output is correct
20 Correct 483 ms 379568 KB Output is correct
21 Correct 524 ms 390740 KB Output is correct
22 Correct 444 ms 363348 KB Output is correct
23 Correct 784 ms 442540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 672 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 1272 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 600 KB Output is correct
36 Correct 1 ms 856 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 1 ms 860 KB Output is correct
39 Correct 1 ms 856 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 856 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 479 ms 373292 KB Output is correct
46 Correct 483 ms 379568 KB Output is correct
47 Correct 524 ms 390740 KB Output is correct
48 Correct 444 ms 363348 KB Output is correct
49 Correct 784 ms 442540 KB Output is correct
50 Correct 687 ms 377264 KB Output is correct
51 Correct 714 ms 371500 KB Output is correct
52 Correct 686 ms 358556 KB Output is correct
53 Correct 751 ms 433336 KB Output is correct
54 Correct 827 ms 441588 KB Output is correct
55 Correct 740 ms 433336 KB Output is correct
56 Correct 825 ms 441520 KB Output is correct
57 Correct 782 ms 441520 KB Output is correct
58 Correct 761 ms 441404 KB Output is correct
59 Correct 731 ms 464724 KB Output is correct
60 Correct 688 ms 424972 KB Output is correct
61 Correct 693 ms 409796 KB Output is correct