//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
struct Tree{
vector<int>tab;
int size = 1;
Tree(int n){
while (size < n) size*=2;
tab.assign(2*size, 0);
}
void update(int x, int v){
x += size;
tab[x] += v;
while (x){
x/=2;
tab[x] = tab[2*x] + tab[2*x+1];
}
}
int query(int l, int r){
int ans = 0;
for (l += size-1, r += size+1; r-l>1; l/=2, r/=2){
if (!(l&1)) ans += tab[l+1];
if (r&1) ans += tab[r-1];
}
return ans;
}
};
void solve(){
int n; cin >> n;
int N = 2*n;
int ans = 0;
vector<T>a(N);
vector cnt(n+2, vector<int>(3));
for (auto &[x, y] : a){
cin >> x >> y;
if (x < 1) {
ans += (1-x);
x = 1;
}
if (x > n){
ans += (x - n);
x = n;
}
if (y < 1){
ans += (1 - y);
y = 1;
}
if (y > 2){
ans += (y - 2);
y = 2;
}
cnt[x][y-1]++;
}
debug(ans);
for (int j = 1; j>=0; j--){
for (int i = 1; i<=n; i++){
cerr << cnt[i][j] << " ";
}
cerr << "\n";
}
vector<int>leave(2);
for (int what = 0; what < 2; what++){
vector<int>ord;
int sgn;
if (!what) {
for (int i = 1; i<=n; i++) ord.emplace_back(i);
sgn = 1;
} else {
for (int i = n; i>=1; i--) ord.emplace_back(i);
sgn = -1;
}
for (auto i: ord){
debug(i, ans, cnt[i][0], cnt[i][1]);
int d = cnt[i][0] + cnt[i][1];
if (d < 2){
//musimy znalezc cos zeby przyciagnac
for (int rep = 0; rep < 2; rep++){
if (!cnt[i][rep]){
leave[rep]++;
}
}
continue;
}
//d >= 2
vector<int>mv(2);
for (int rep = 0; rep < 2; rep++){
int t = min(leave[rep], max(0ll, cnt[i][rep]-1)); //t zostaje w miejscu, przesuwamy je pozniej w lewo na tym samym lvl
leave[rep] -= t;
ans += t;
cnt[i-sgn][rep] += t;
cnt[i][rep] -= t;
mv[rep] = max(0ll, cnt[i][rep]-1);
}
debug(cnt[i][0], cnt[i][1]);
debug(mv);
for (int rep = 0; rep < 2; rep++){
if (leave[rep]){
int t = min(mv[rep^1], leave[rep]);
//tyle mozemy ukrasc i przesunac w pionie, by potem miec na lewo
ans += 2*t;
cnt[i-sgn][rep] += t;
cnt[i][rep^1] -= t;
leave[rep] -= t;
}
}
d = cnt[i][0] + cnt[i][1];
if (d < 2){
for (int rep = 0; rep < 2; rep++){
if (!cnt[i][rep]){
leave[rep]++;
}
}
continue;
}
for (int rep = 0; rep < 2; rep++){
if (!cnt[i][rep] && mv[rep^1]){
ans++;
cnt[i][rep]++;
cnt[i][rep^1]--;
}
}
for (int rep = 0; rep < 2; rep++){
cnt[i+sgn][rep] += (cnt[i][rep]-1);
ans += (cnt[i][rep]-1);
cnt[i][rep] = 1;
}
debug(cnt[i][0], cnt[i][1]);
}
for (int j = 1; j>=0; j--){
for (int i = 1; i<=n; i++){
cerr << cnt[i][j] << " ";
}
cerr << "\n";
}
debug(ans);
// return;
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
12 ms |
452 KB |
Output is correct |
35 |
Correct |
12 ms |
468 KB |
Output is correct |
36 |
Correct |
12 ms |
468 KB |
Output is correct |
37 |
Correct |
12 ms |
468 KB |
Output is correct |
38 |
Correct |
13 ms |
464 KB |
Output is correct |
39 |
Correct |
12 ms |
440 KB |
Output is correct |
40 |
Correct |
12 ms |
456 KB |
Output is correct |
41 |
Correct |
12 ms |
440 KB |
Output is correct |
42 |
Correct |
12 ms |
444 KB |
Output is correct |
43 |
Correct |
12 ms |
472 KB |
Output is correct |
44 |
Correct |
12 ms |
468 KB |
Output is correct |
45 |
Correct |
12 ms |
468 KB |
Output is correct |
46 |
Correct |
12 ms |
340 KB |
Output is correct |
47 |
Correct |
12 ms |
468 KB |
Output is correct |
48 |
Correct |
15 ms |
464 KB |
Output is correct |
49 |
Correct |
12 ms |
436 KB |
Output is correct |
50 |
Correct |
12 ms |
464 KB |
Output is correct |
51 |
Correct |
14 ms |
468 KB |
Output is correct |
52 |
Correct |
12 ms |
468 KB |
Output is correct |
53 |
Correct |
12 ms |
440 KB |
Output is correct |
54 |
Correct |
12 ms |
476 KB |
Output is correct |
55 |
Correct |
12 ms |
436 KB |
Output is correct |
56 |
Correct |
12 ms |
464 KB |
Output is correct |
57 |
Correct |
12 ms |
468 KB |
Output is correct |
58 |
Correct |
12 ms |
472 KB |
Output is correct |
59 |
Correct |
12 ms |
472 KB |
Output is correct |
60 |
Correct |
12 ms |
468 KB |
Output is correct |
61 |
Correct |
15 ms |
340 KB |
Output is correct |
62 |
Correct |
12 ms |
460 KB |
Output is correct |
63 |
Correct |
12 ms |
468 KB |
Output is correct |
64 |
Correct |
16 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
12 ms |
452 KB |
Output is correct |
35 |
Correct |
12 ms |
468 KB |
Output is correct |
36 |
Correct |
12 ms |
468 KB |
Output is correct |
37 |
Correct |
12 ms |
468 KB |
Output is correct |
38 |
Correct |
13 ms |
464 KB |
Output is correct |
39 |
Correct |
12 ms |
440 KB |
Output is correct |
40 |
Correct |
12 ms |
456 KB |
Output is correct |
41 |
Correct |
12 ms |
440 KB |
Output is correct |
42 |
Correct |
12 ms |
444 KB |
Output is correct |
43 |
Correct |
12 ms |
472 KB |
Output is correct |
44 |
Correct |
12 ms |
468 KB |
Output is correct |
45 |
Correct |
12 ms |
468 KB |
Output is correct |
46 |
Correct |
12 ms |
340 KB |
Output is correct |
47 |
Correct |
12 ms |
468 KB |
Output is correct |
48 |
Correct |
15 ms |
464 KB |
Output is correct |
49 |
Correct |
12 ms |
436 KB |
Output is correct |
50 |
Correct |
12 ms |
464 KB |
Output is correct |
51 |
Correct |
14 ms |
468 KB |
Output is correct |
52 |
Correct |
12 ms |
468 KB |
Output is correct |
53 |
Correct |
12 ms |
440 KB |
Output is correct |
54 |
Correct |
12 ms |
476 KB |
Output is correct |
55 |
Correct |
12 ms |
436 KB |
Output is correct |
56 |
Correct |
12 ms |
464 KB |
Output is correct |
57 |
Correct |
12 ms |
468 KB |
Output is correct |
58 |
Correct |
12 ms |
472 KB |
Output is correct |
59 |
Correct |
12 ms |
472 KB |
Output is correct |
60 |
Correct |
12 ms |
468 KB |
Output is correct |
61 |
Correct |
15 ms |
340 KB |
Output is correct |
62 |
Correct |
12 ms |
460 KB |
Output is correct |
63 |
Correct |
12 ms |
468 KB |
Output is correct |
64 |
Correct |
16 ms |
468 KB |
Output is correct |
65 |
Execution timed out |
1081 ms |
13380 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |