#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e18 + 69;
int n;
pa a[maxn];
struct sub2 {
static const int maxn = 2e3 + 7;
ll dp[maxn][maxn];
void solve() {
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; i++) cout<<a[i].fi<<" "<<a[i].se<<"\n";
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) dp[i][j] = oo;
}
dp[0][0] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= min(i, n / 2); j++) {
int rem = i - j;
if(rem > n / 2) continue;
if(j > 0) {
mini(dp[i][j], dp[i - 1][j - 1] + abs(a[i].fi - j) + abs(a[i].se - 1));
}
if(rem > 0) {
mini(dp[i][j], dp[i - 1][j] + abs(a[i].fi - rem) + abs(a[i].se - 2));
}
}
}
cout<<dp[n][n / 2];
}
} sub2;
struct sub3 {
void solve() {
sort(a + 1, a + 1 + n);
ll ans = 0;
int cnt = 0;
for(int i = 1; i <= n; i++) {
ans += abs(a[i].fi - (i + 1) / 2) + abs(a[i].se - 1);
if( a[i].se > 1 && cnt < n / 2) {
ans--;
cnt++;
}
}
ans += n / 2 - cnt;
cout<<ans;
}
} sub3;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n;
n *= 2;
for(int i = 1; i <= n; i++) cin>>a[i].fi>>a[i].se;
// sub2.solve();
sub3.solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |