# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
125275 | wilwxk | Coin Collecting (JOI19_ho_t4) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e3+5;
const ll INF=1e15+9;
vector<pair<ll, ll> > v[2];
ll dp[MAXN][MAXN];
int n;
ll dist(int a, int b, ll x) {
if(b>=v[a].size()) return INF;
ll resp=0;
auto ponto=v[a][b];
resp+=llabs(ponto.first-x);
resp+=min(llabs(ponto.second-2), abs(llponto.second-1));
return resp;
}
ll fazdp(int a, int b) {
if(a==v[0].size()&&b==v[1].size()) return 0;
if(a>v[0].size()||b>v[1].size()) return INF;
if(dp[a][b]!=-1) return dp[a][b];
// printf("calc %d %d\n", a, b);
ll ind=(a+b)/2+1;
dp[a][b]=INF;
ll &cur=dp[a][b];
cur=min(cur, fazdp(a+2, b)+dist(0, a, ind)+dist(0, a+1, ind)+1);
cur=min(cur, fazdp(a, b+2)+dist(1, b, ind)+dist(1, b+1, ind)+1);
cur=min(cur, fazdp(a+1, b+1)+dist(0, a, ind)+dist(1, b, ind));
// printf("%d %d >> %lld\n", a, b, cur);
return cur;
}
int main() {
scanf("%d", &n); n*=2;
memset(dp, -1, sizeof(dp));
for(int i=1; i<=n; i++) {
int a, b; scanf("%d %d", &a, &b);
if(b>=2) v[0].push_back({a, b});
else v[1].push_back({a, b});
}
sort(v[0].begin(), v[0].end());
sort(v[1].begin(), v[1].end());
ll respf=fazdp(0, 0);
printf("%lld\n", respf);
}