Submission #889304

# Submission time Handle Problem Language Result Execution time Memory
889304 2023-12-19T10:17:57 Z hafo Coin Collecting (JOI19_ho_t4) C++14
100 / 100
40 ms 7532 KB
#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 {
    int cnt[maxn][2], d[2];

    void solve() {
        ll ans = 0;
        for(int i = 1; i <= n; i++) {
            int x = a[i].fi, y = a[i].se;
            if(x < 1) {
                ans += 1 - x;
                x = 1;
            }
            if(x > n / 2) {
                ans += x - n / 2;
                x = n / 2;
            }
            if(y < 1) {
                ans += 1 - y;
                y = 1;
            }
            if(y > 2) {
                ans += y - 2;
                y = 2;
            }

            cnt[x][y - 1]++;
        }

        for(int i = 1; i <= n / 2; i++) {
            for(int j = 0; j < 2; j++) d[j] += cnt[i][j] - 1;
            
            for(int j = 0; j < 2; j++) {
                if(d[j] > 0 && d[j ^ 1] < 0) {
                    int t = min(d[j], - d[j ^ 1]);
                    ans += t;
                    d[j] -= t;
                    d[j ^ 1] += t;
                }
            }

            for(int j = 0; j < 2; j++) ans += abs(d[j]);
        }
        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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 528 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 476 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 344 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 528 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 476 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 344 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 20 ms 5648 KB Output is correct
66 Correct 36 ms 6664 KB Output is correct
67 Correct 30 ms 7260 KB Output is correct
68 Correct 30 ms 7404 KB Output is correct
69 Correct 40 ms 7248 KB Output is correct
70 Correct 30 ms 7252 KB Output is correct
71 Correct 29 ms 7312 KB Output is correct
72 Correct 30 ms 7188 KB Output is correct
73 Correct 31 ms 7092 KB Output is correct
74 Correct 30 ms 7236 KB Output is correct
75 Correct 30 ms 6968 KB Output is correct
76 Correct 30 ms 7256 KB Output is correct
77 Correct 38 ms 6992 KB Output is correct
78 Correct 29 ms 7124 KB Output is correct
79 Correct 30 ms 7120 KB Output is correct
80 Correct 32 ms 7100 KB Output is correct
81 Correct 33 ms 7252 KB Output is correct
82 Correct 30 ms 7148 KB Output is correct
83 Correct 30 ms 7168 KB Output is correct
84 Correct 31 ms 7252 KB Output is correct
85 Correct 30 ms 7252 KB Output is correct
86 Correct 37 ms 7532 KB Output is correct
87 Correct 29 ms 7248 KB Output is correct
88 Correct 30 ms 7252 KB Output is correct
89 Correct 33 ms 7224 KB Output is correct
90 Correct 35 ms 7252 KB Output is correct
91 Correct 29 ms 7252 KB Output is correct
92 Correct 29 ms 7504 KB Output is correct
93 Correct 29 ms 7252 KB Output is correct
94 Correct 28 ms 7248 KB Output is correct
95 Correct 29 ms 7180 KB Output is correct
96 Correct 30 ms 7096 KB Output is correct
97 Correct 29 ms 7260 KB Output is correct
98 Correct 36 ms 7252 KB Output is correct
99 Correct 37 ms 7036 KB Output is correct
100 Correct 30 ms 7076 KB Output is correct
101 Correct 29 ms 7244 KB Output is correct
102 Correct 29 ms 7256 KB Output is correct
103 Correct 32 ms 7192 KB Output is correct