Submission #1873067


Source Code Expand

#include<bits/stdc++.h>
#define out(x) cerr << #x << " = " << x << "\n"
using namespace std;
// by piano
template<typename tp> inline void read(tp &x) {
  x = 0;char c = getchar();bool f = 0;
  for(; c < '0' || c > '9'; f |= (c == '-'), c = getchar());
  for(; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
  if(f) x = -x;
}
#define c(a, b) memset(a, b, sizeof(a))
const int N = 3e5 + 233, inf = 1e9 + 233;
int n, Q, x, y, a[N], ans, tag, res[N];
struct T {
  int tr[N << 2], M;
  inline void init() {
    for(M = 1; M <= n + 1; M <<= 1);
    memset(tr, 38, sizeof(tr));
  }
  inline void ps(int &x, int y) {if(x > y) x = y;}
  inline void in(int u, int v) {
    for(ps(tr[u += M], v), u >>= 1; u; ps(tr[u >>= 1], v));
  }
  inline int q(int s, int t, int ans = inf) {
    for(s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
      if(~s & 1) ps(ans, tr[s ^ 1]);
      if(t & 1) ps(ans, tr[t ^ 1]);
    }
    return ans;
  }
}tr1, tr2;
  
inline void add2(int pos, int now) {
  now += (n - (pos - 1));
  // cout << "tr2 " << pos << " " << now << "\n";
  tr2.in(pos, now);
} 

inline void add1(int pos, int now) {
  now += pos; 
  // cout << "tr1 " << pos << " " << now << "\n";
  tr1.in(pos, now);
}

inline void solve(int pos, int pre) {
  tag += abs(pos - pre);
  int l = tr2.q(1, pos), r = tr1.q(pos, n);
  l -= (n - (pos - 1)); r -= pos;
  // cout << l << " " << r << "\n";
  int now = min(l, r);
  now -= abs(pos - pre);
  add1(pre, now);
  add2(pre, now);
  // for(int i = 1; i <= n; i ++)
  //   cout << tr1.q(i, i) - i + tag << " ";
  // putchar('\n');
}

main() {
  read(n); read(Q); read(x); read(y);
  tr1.init(); tr2.init();
  add1(y, 0); add2(y, 0);
  a[0] = x;
  for(int i = 1; i <= Q; i ++) read(a[i]);
  for(int i = 1; i <= Q; i ++) solve(a[i], a[i - 1]);
  ans = inf;
  for(int i = 1; i <= n; i ++) {
    res[i] = tr1.q(i, i) - i;
    ans = min(ans, res[i]);
  }
  cout << ans + tag << "\n";
}

Submission Info

Submission Time
Task F - Many Moves
User foreverpiano
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2016 Byte
Status WA
Exec Time 61 ms
Memory 11264 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 2
WA × 2
AC × 5
WA × 29
Set Name Test Cases
Sample example0, example1, example2, example3
All example0, example1, example2, example3, flower0, flower1, flower2, flower3, flower4, flower5, flower6, flower7, flower8, flower9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, smallrand5, smallrand6, smallrand7, smallrand8, smallrand9
Case Name Status Exec Time Memory
example0 AC 4 ms 10496 KB
example1 WA 4 ms 10496 KB
example2 AC 4 ms 10496 KB
example3 WA 4 ms 10496 KB
flower0 WA 58 ms 11264 KB
flower1 WA 58 ms 11264 KB
flower2 WA 58 ms 11264 KB
flower3 WA 57 ms 11264 KB
flower4 WA 58 ms 11264 KB
flower5 WA 58 ms 11264 KB
flower6 WA 58 ms 11264 KB
flower7 WA 58 ms 11264 KB
flower8 WA 58 ms 11264 KB
flower9 WA 58 ms 11264 KB
maxrand0 WA 61 ms 11264 KB
maxrand1 WA 61 ms 11264 KB
maxrand2 WA 61 ms 11264 KB
maxrand3 WA 61 ms 11264 KB
maxrand4 WA 61 ms 11264 KB
maxrand5 WA 61 ms 11264 KB
maxrand6 WA 61 ms 11264 KB
maxrand7 WA 61 ms 11264 KB
maxrand8 WA 61 ms 11264 KB
maxrand9 WA 61 ms 11264 KB
smallrand0 WA 4 ms 10496 KB
smallrand1 WA 4 ms 10496 KB
smallrand2 WA 4 ms 10496 KB
smallrand3 WA 4 ms 10496 KB
smallrand4 WA 4 ms 10496 KB
smallrand5 WA 4 ms 10496 KB
smallrand6 AC 4 ms 10496 KB
smallrand7 WA 4 ms 10496 KB
smallrand8 AC 4 ms 10496 KB
smallrand9 AC 4 ms 10496 KB