Submission #1161811

#TimeUsernameProblemLanguageResultExecution timeMemory
1161811Zero_OPCoin Collecting (JOI19_ho_t4)C++20
37 / 100
180 ms339968 KiB
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i)

#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define compact(v) v.erase(unique(all(v)), end(v))

#define dbg(x) "[" #x " = " << (x) << "]"

template<typename T>
bool minimize(T& a, const T& b){
      if(a > b) return a = b, true;
      return false;
}

template<typename T>
bool maximize(T& a, const T& b){
      if(a < b) return a = b, true;
      return false;
}

using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vc = vector<char>;
using vd = vector<db>;

using vpi = vector<pi>;
using vpl = vector<pl>;

void setIO(){
      ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
      freopen("task.inp", "r", stdin);
      freopen("task.out", "w", stdout);
#endif // LOCAL
}

ll dist(ll x1, ll y1, ll x2, ll y2){
      return abs(x1 - x2) + abs(y1 - y2);
}

int main(){
      setIO();

      int N;
      cin >> N;

      vi X(2 * N), Y(2 * N);
      ll base = 0;

      FOR(i, 0, 2*N){
            cin >> X[i] >> Y[i];

            tuple<ll, int, int> best(1e18, 0, 0);
            if(1 <= X[i] && X[i] <= N){
                  tuple<ll, int, int> U(dist(X[i], 2, X[i], Y[i]), X[i], 2);
                  tuple<ll, int, int> D(dist(X[i], 1, X[i], Y[i]), X[i], 1);
                  best = min(U, D);
            } else{
                  tuple<ll, int, int> DL(dist(1, 1, X[i], Y[i]), 1, 1);
                  tuple<ll, int, int> DR(dist(N, 1, X[i], Y[i]), N, 1);
                  tuple<ll, int, int> UL(dist(1, 2, X[i], Y[i]), 1, 2);
                  tuple<ll, int, int> UR(dist(N, 2, X[i], Y[i]), N, 2);

                  best = min({DL, DR, UL, UR});
            }

            ll cost; int x, y;
            tie(cost, x, y) = best;
            X[i] = x;
            Y[i] = y;
            base += cost;
      }

      vector<array<int, 2>> cnt(N);

//      cout << base << '\n';
      FOR(i, 0, 2 * N){
            ++cnt[X[i]-1][Y[i]-1];
      }

      //simulate the greedy
      queue<int> need[2], have[2];

      //only (need[0] need[1] empty) or (have[0] have[1] empty)
//      FOR(j, 0, 2) FOR(i, 0, N){
//            cout << cnt[i][j] << " \n"[i == N-1];
//      }

      vi idx(2 * N);
      iota(all(idx), 0);

      sort(all(idx), [&](int i, int j){ return mp(X[i], Y[i]) < mp(X[j], Y[j]); });

      vector<vl> dp(N+1, vl(N+1, 1e18));
      dp[0][0] = 0;
      int num = 0;
      for(auto i : idx){
            FOR(a, 0, N+1){
                  int b = num-a;
                  if(0 <= b && b <= N){
                        if(a<N) minimize(dp[a+1][b], dp[a][b] + abs(a+1 - X[i]) + (Y[i] == 2));
                        if(b<N) minimize(dp[a][b+1], dp[a][b] + abs(b+1 - X[i]) + (Y[i] == 1));
                  }
            }

            ++num;
      }

      cout << base + dp[N][N] << '\n';

      return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...