#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
using lint = long long;
using pi = pair<int, int>;
struct pnt{
int x, y;
bool operator<(const pnt &p)const{
return pi(x, y) < pi(p.x, p.y);
}
}a[MAXN];
int n;
int cnt[3][MAXN];
lint dp[MAXN][50];
lint sum(int x){ return 1ll * x * (x + 1) / 2; }
lint sum_intv(int l, int r, int x){
if(l <= x && x < r){
return sum(x - l) + sum(r - 1 - x);
}
if(x >= r) return sum(x - l) - sum(x - r);
return sum(r - x - 1) - sum(l - x - 1);
}
int thres[MAXN];
map<pi, lint> mp;
bool insec(int l, int r, int ll, int rr){
return max(l, ll) <= min(r, rr) + 10;
}
lint f(int l, int r, int pos){
if(pos == n + 1) return l == r ? 0 : 1e18;
if(cnt[1][pos] == 0 && cnt[2][pos] == 0) return f(l, r, pos + 1);
if(mp.find(pi(l, r)) != mp.end()) return mp[pi(l, r)];
int up = cnt[1][pos];
int dn = cnt[2][pos];
lint ret = 1e18;
for(int i=0; i<=up+dn; i++){
int nl = l + i, nr = up + dn - i + r;
if(nl > n + 1 || nr > n + 1) continue;
if(i > 0 && i < up + dn && i != up && !insec(l, nl, r, nr)) continue;
lint cost = f(nl, nr, pos + 1);
cost += abs(up - (nl - l));
cost += sum_intv(l, nl, pos);
cost += sum_intv(r, nr, pos);
ret = min(ret, cost);
}
return mp[pi(l, r)] = ret;
}
int main(){
scanf("%d",&n);
memset(dp, -1, sizeof(dp));
lint cdap = 0;
for(int i=0; i<n*2; i++){
scanf("%d %d",&a[i].x,&a[i].y);
if(a[i].x < 1){
cdap += 1 - a[i].x;
a[i].x = 1;
}
if(a[i].x > n){
cdap += a[i].x - n;
a[i].x = n;
}
if(a[i].y < 1){
cdap += 1 - a[i].y;
a[i].y = 1;
}
if(a[i].y > 2){
cdap += a[i].y - 2;
a[i].y = 2;
}
cnt[a[i].y][a[i].x]++;
}
for(int i=1; i<=n; i++){
for(int j=-2; j<=2; j++){
if(i + j >= 1 && i + j <= n){
thres[i] += cnt[1][i+j] + cnt[2][i+j];
}
}
}
sort(a, a + n + n);
cout << cdap + f(1, 1, 1) << endl;
}
Compilation message
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
joi2019_ho_t4.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&a[i].x,&a[i].y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
78588 KB |
Output is correct |
2 |
Correct |
65 ms |
78588 KB |
Output is correct |
3 |
Correct |
75 ms |
78556 KB |
Output is correct |
4 |
Correct |
63 ms |
78592 KB |
Output is correct |
5 |
Correct |
70 ms |
78584 KB |
Output is correct |
6 |
Correct |
75 ms |
78584 KB |
Output is correct |
7 |
Correct |
65 ms |
78544 KB |
Output is correct |
8 |
Correct |
66 ms |
78636 KB |
Output is correct |
9 |
Correct |
67 ms |
78584 KB |
Output is correct |
10 |
Correct |
73 ms |
78576 KB |
Output is correct |
11 |
Correct |
76 ms |
78584 KB |
Output is correct |
12 |
Correct |
66 ms |
78680 KB |
Output is correct |
13 |
Correct |
68 ms |
78712 KB |
Output is correct |
14 |
Correct |
72 ms |
78632 KB |
Output is correct |
15 |
Correct |
71 ms |
78544 KB |
Output is correct |
16 |
Correct |
68 ms |
78584 KB |
Output is correct |
17 |
Correct |
65 ms |
78556 KB |
Output is correct |
18 |
Correct |
61 ms |
78584 KB |
Output is correct |
19 |
Correct |
59 ms |
78584 KB |
Output is correct |
20 |
Correct |
66 ms |
78760 KB |
Output is correct |
21 |
Correct |
59 ms |
78624 KB |
Output is correct |
22 |
Correct |
74 ms |
78612 KB |
Output is correct |
23 |
Correct |
63 ms |
78584 KB |
Output is correct |
24 |
Correct |
61 ms |
78584 KB |
Output is correct |
25 |
Correct |
82 ms |
78604 KB |
Output is correct |
26 |
Correct |
59 ms |
78584 KB |
Output is correct |
27 |
Correct |
63 ms |
78612 KB |
Output is correct |
28 |
Correct |
67 ms |
78644 KB |
Output is correct |
29 |
Correct |
66 ms |
78556 KB |
Output is correct |
30 |
Correct |
78 ms |
78584 KB |
Output is correct |
31 |
Correct |
63 ms |
78540 KB |
Output is correct |
32 |
Correct |
62 ms |
78572 KB |
Output is correct |
33 |
Correct |
66 ms |
78664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
78588 KB |
Output is correct |
2 |
Correct |
65 ms |
78588 KB |
Output is correct |
3 |
Correct |
75 ms |
78556 KB |
Output is correct |
4 |
Correct |
63 ms |
78592 KB |
Output is correct |
5 |
Correct |
70 ms |
78584 KB |
Output is correct |
6 |
Correct |
75 ms |
78584 KB |
Output is correct |
7 |
Correct |
65 ms |
78544 KB |
Output is correct |
8 |
Correct |
66 ms |
78636 KB |
Output is correct |
9 |
Correct |
67 ms |
78584 KB |
Output is correct |
10 |
Correct |
73 ms |
78576 KB |
Output is correct |
11 |
Correct |
76 ms |
78584 KB |
Output is correct |
12 |
Correct |
66 ms |
78680 KB |
Output is correct |
13 |
Correct |
68 ms |
78712 KB |
Output is correct |
14 |
Correct |
72 ms |
78632 KB |
Output is correct |
15 |
Correct |
71 ms |
78544 KB |
Output is correct |
16 |
Correct |
68 ms |
78584 KB |
Output is correct |
17 |
Correct |
65 ms |
78556 KB |
Output is correct |
18 |
Correct |
61 ms |
78584 KB |
Output is correct |
19 |
Correct |
59 ms |
78584 KB |
Output is correct |
20 |
Correct |
66 ms |
78760 KB |
Output is correct |
21 |
Correct |
59 ms |
78624 KB |
Output is correct |
22 |
Correct |
74 ms |
78612 KB |
Output is correct |
23 |
Correct |
63 ms |
78584 KB |
Output is correct |
24 |
Correct |
61 ms |
78584 KB |
Output is correct |
25 |
Correct |
82 ms |
78604 KB |
Output is correct |
26 |
Correct |
59 ms |
78584 KB |
Output is correct |
27 |
Correct |
63 ms |
78612 KB |
Output is correct |
28 |
Correct |
67 ms |
78644 KB |
Output is correct |
29 |
Correct |
66 ms |
78556 KB |
Output is correct |
30 |
Correct |
78 ms |
78584 KB |
Output is correct |
31 |
Correct |
63 ms |
78540 KB |
Output is correct |
32 |
Correct |
62 ms |
78572 KB |
Output is correct |
33 |
Correct |
66 ms |
78664 KB |
Output is correct |
34 |
Correct |
653 ms |
110108 KB |
Output is correct |
35 |
Correct |
58 ms |
78712 KB |
Output is correct |
36 |
Correct |
758 ms |
105840 KB |
Output is correct |
37 |
Correct |
719 ms |
105848 KB |
Output is correct |
38 |
Correct |
771 ms |
106552 KB |
Output is correct |
39 |
Correct |
708 ms |
106388 KB |
Output is correct |
40 |
Correct |
714 ms |
105352 KB |
Output is correct |
41 |
Correct |
63 ms |
78968 KB |
Output is correct |
42 |
Correct |
61 ms |
78712 KB |
Output is correct |
43 |
Correct |
68 ms |
78880 KB |
Output is correct |
44 |
Correct |
68 ms |
78840 KB |
Output is correct |
45 |
Correct |
98 ms |
78840 KB |
Output is correct |
46 |
Correct |
290 ms |
78968 KB |
Output is correct |
47 |
Correct |
188 ms |
79192 KB |
Output is correct |
48 |
Correct |
200 ms |
79096 KB |
Output is correct |
49 |
Correct |
145 ms |
81784 KB |
Output is correct |
50 |
Correct |
144 ms |
81804 KB |
Output is correct |
51 |
Correct |
420 ms |
90840 KB |
Output is correct |
52 |
Correct |
392 ms |
90708 KB |
Output is correct |
53 |
Correct |
599 ms |
105976 KB |
Output is correct |
54 |
Correct |
685 ms |
106576 KB |
Output is correct |
55 |
Correct |
801 ms |
105620 KB |
Output is correct |
56 |
Correct |
639 ms |
105888 KB |
Output is correct |
57 |
Correct |
141 ms |
82168 KB |
Output is correct |
58 |
Correct |
286 ms |
89048 KB |
Output is correct |
59 |
Correct |
122 ms |
82396 KB |
Output is correct |
60 |
Correct |
148 ms |
82716 KB |
Output is correct |
61 |
Correct |
218 ms |
84876 KB |
Output is correct |
62 |
Correct |
164 ms |
84148 KB |
Output is correct |
63 |
Correct |
158 ms |
82940 KB |
Output is correct |
64 |
Correct |
115 ms |
81528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
78588 KB |
Output is correct |
2 |
Correct |
65 ms |
78588 KB |
Output is correct |
3 |
Correct |
75 ms |
78556 KB |
Output is correct |
4 |
Correct |
63 ms |
78592 KB |
Output is correct |
5 |
Correct |
70 ms |
78584 KB |
Output is correct |
6 |
Correct |
75 ms |
78584 KB |
Output is correct |
7 |
Correct |
65 ms |
78544 KB |
Output is correct |
8 |
Correct |
66 ms |
78636 KB |
Output is correct |
9 |
Correct |
67 ms |
78584 KB |
Output is correct |
10 |
Correct |
73 ms |
78576 KB |
Output is correct |
11 |
Correct |
76 ms |
78584 KB |
Output is correct |
12 |
Correct |
66 ms |
78680 KB |
Output is correct |
13 |
Correct |
68 ms |
78712 KB |
Output is correct |
14 |
Correct |
72 ms |
78632 KB |
Output is correct |
15 |
Correct |
71 ms |
78544 KB |
Output is correct |
16 |
Correct |
68 ms |
78584 KB |
Output is correct |
17 |
Correct |
65 ms |
78556 KB |
Output is correct |
18 |
Correct |
61 ms |
78584 KB |
Output is correct |
19 |
Correct |
59 ms |
78584 KB |
Output is correct |
20 |
Correct |
66 ms |
78760 KB |
Output is correct |
21 |
Correct |
59 ms |
78624 KB |
Output is correct |
22 |
Correct |
74 ms |
78612 KB |
Output is correct |
23 |
Correct |
63 ms |
78584 KB |
Output is correct |
24 |
Correct |
61 ms |
78584 KB |
Output is correct |
25 |
Correct |
82 ms |
78604 KB |
Output is correct |
26 |
Correct |
59 ms |
78584 KB |
Output is correct |
27 |
Correct |
63 ms |
78612 KB |
Output is correct |
28 |
Correct |
67 ms |
78644 KB |
Output is correct |
29 |
Correct |
66 ms |
78556 KB |
Output is correct |
30 |
Correct |
78 ms |
78584 KB |
Output is correct |
31 |
Correct |
63 ms |
78540 KB |
Output is correct |
32 |
Correct |
62 ms |
78572 KB |
Output is correct |
33 |
Correct |
66 ms |
78664 KB |
Output is correct |
34 |
Correct |
653 ms |
110108 KB |
Output is correct |
35 |
Correct |
58 ms |
78712 KB |
Output is correct |
36 |
Correct |
758 ms |
105840 KB |
Output is correct |
37 |
Correct |
719 ms |
105848 KB |
Output is correct |
38 |
Correct |
771 ms |
106552 KB |
Output is correct |
39 |
Correct |
708 ms |
106388 KB |
Output is correct |
40 |
Correct |
714 ms |
105352 KB |
Output is correct |
41 |
Correct |
63 ms |
78968 KB |
Output is correct |
42 |
Correct |
61 ms |
78712 KB |
Output is correct |
43 |
Correct |
68 ms |
78880 KB |
Output is correct |
44 |
Correct |
68 ms |
78840 KB |
Output is correct |
45 |
Correct |
98 ms |
78840 KB |
Output is correct |
46 |
Correct |
290 ms |
78968 KB |
Output is correct |
47 |
Correct |
188 ms |
79192 KB |
Output is correct |
48 |
Correct |
200 ms |
79096 KB |
Output is correct |
49 |
Correct |
145 ms |
81784 KB |
Output is correct |
50 |
Correct |
144 ms |
81804 KB |
Output is correct |
51 |
Correct |
420 ms |
90840 KB |
Output is correct |
52 |
Correct |
392 ms |
90708 KB |
Output is correct |
53 |
Correct |
599 ms |
105976 KB |
Output is correct |
54 |
Correct |
685 ms |
106576 KB |
Output is correct |
55 |
Correct |
801 ms |
105620 KB |
Output is correct |
56 |
Correct |
639 ms |
105888 KB |
Output is correct |
57 |
Correct |
141 ms |
82168 KB |
Output is correct |
58 |
Correct |
286 ms |
89048 KB |
Output is correct |
59 |
Correct |
122 ms |
82396 KB |
Output is correct |
60 |
Correct |
148 ms |
82716 KB |
Output is correct |
61 |
Correct |
218 ms |
84876 KB |
Output is correct |
62 |
Correct |
164 ms |
84148 KB |
Output is correct |
63 |
Correct |
158 ms |
82940 KB |
Output is correct |
64 |
Correct |
115 ms |
81528 KB |
Output is correct |
65 |
Execution timed out |
1074 ms |
139652 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |