#include<bits/stdc++.h>
#pragma optimize ("g",on)
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
//01001101 01100001 01101011 01101000 01100001 01100111 01100001 01111001
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e15, N = 1e6 + 1, mod = 998244353;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int dp[1001][2001];
main() {
//freopen("cowrect.in", "r", stdin);
//freopen("cowrect.out", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int n;
cin >> n;
vector<pii> a, b;
for (int i = 1, x, y; i <= n * 2; i++) {
cin >> x >> y;
if (y <= 1) {
a.pb({x, y});
}else b.pb({x, y});
}
if (sz(a) > n) {
sort(all(a));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sz(a); j++) dp[i][j] = inf;
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sz(a); j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(a[j - 1].ff - i) + 1 - a[j - 1].ss);
}
}
set<int> pos;
for (int i = 0; i < sz(a); i++) pos.insert(i);
int x = n, y = sz(a);
while (x && y) {
if (dp[x][y] == dp[x][y - 1]) {
y--;
continue;
}
x--, y--;
pos.erase(y);
}
for (auto e: pos) {
b.pb(a[e]);
}
int cur = dp[n][sz(a)];
sort(all(b));
for (int i = 0; i < sz(b); i++) {
cur += abs(i + 1 - b[i].ff) + abs(2 - b[i].ss);
}
cout << cur;
}else {
sort(all(b));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= sz(b); j++) dp[i][j] = inf;
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sz(b); j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(b[j - 1].ff - i) + b[j - 1].ss - 2);
}
}
set<int> pos;
for (int i = 0; i < sz(b); i++) pos.insert(i);
int x = n, y = sz(b);
while (x && y) {
if (dp[x][y] == dp[x][y - 1]) {
y--;
continue;
}
x--, y--;
pos.erase(y);
}
for (auto e: pos) {
a.pb(b[e]);
}
int cur = dp[n][sz(b)];
sort(all(a));
for (int i = 0; i < sz(a); i++) {
cur += abs(i + 1 - a[i].ff) + abs(1 - a[i].ss);
}
cout << cur;
}
}
Compilation message
joi2019_ho_t4.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize ("g",on)
|
joi2019_ho_t4.cpp:9: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
9 | #pragma comment(linker, "/stack:200000000")
|
joi2019_ho_t4.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
37 | main() {
| ^~~~
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:51:12: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
51 | if (sz(a) > n) {
| ^
joi2019_ho_t4.cpp:54:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int j = 0; j <= sz(a); j++) dp[i][j] = inf;
| ^
joi2019_ho_t4.cpp:58:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j = 1; j <= sz(a); j++) {
| ^
joi2019_ho_t4.cpp:63:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < sz(a); i++) pos.insert(i);
| ^
joi2019_ho_t4.cpp:78:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i < sz(b); i++) {
| ^
joi2019_ho_t4.cpp:85:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int j = 0; j <= sz(b); j++) dp[i][j] = inf;
| ^
joi2019_ho_t4.cpp:89:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int j = 1; j <= sz(b); j++) {
| ^
joi2019_ho_t4.cpp:94:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < sz(b); i++) pos.insert(i);
| ^
joi2019_ho_t4.cpp:109:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i = 0; i < sz(a); i++) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |