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;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
const int LIM=1e5+7, K=100;
const ll INF=1e18+7;
ll ile[LIM][2], dp[LIM][2*K+1];
vector<pair<ll,ll>>V;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll n;
cin >> n;
ll ans=0;
rep(i, 2*n) {
ll a, b;
cin >> a >> b;
ans+=max(1-a, 0ll);
a=max(a, 1ll);
ans+=max(a-n, 0ll);
a=min(a, n);
ans+=max(1-b, 0ll);
b=max(b, 1ll);
ans+=max(b-2, 0ll);
b=min(b, 2ll);
--a; --b;
++ile[a][b];
}
rep(i, n) {
while(ile[i][0] && ile[i][1]) {
V.pb({i, 0});
V.pb({i, 1});
--ile[i][0];
--ile[i][1];
}
while(ile[i][0]) {
V.pb({i, 0});
--ile[i][0];
}
while(ile[i][1]) {
V.pb({i, 1});
--ile[i][1];
}
}
rep(i, V.size()+1) rep(j, 2*K+1) dp[i][j]=INF;
dp[0][K]=0;
rep(i, V.size()) {
rep(j, 2*K+1) {
if((i+1+j-K)%2) continue;
ll x=(i+1+j-K)/2-1;
if(j) {
dp[i+1][j]=min(dp[i+1][j], dp[i][j-1]+abs(V[i].nd-0ll)+abs(V[i].st-x));
}
if(j<2*K) {
dp[i+1][j]=min(dp[i+1][j], dp[i][j+1]+abs(V[i].nd-1ll)+abs(V[i].st-(x-j+K)));
}
}
}
cout << ans+dp[V.size()][K] << '\n';
}
Compilation message (stderr)
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
joi2019_ho_t4.cpp:47:5: note: in expansion of macro 'rep'
47 | rep(i, V.size()+1) rep(j, 2*K+1) dp[i][j]=INF;
| ^~~
joi2019_ho_t4.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
joi2019_ho_t4.cpp:49:5: note: in expansion of macro 'rep'
49 | rep(i, V.size()) {
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |