Submission #330914

# Submission time Handle Problem Language Result Execution time Memory
330914 2020-11-26T21:10:23 Z caoash Coin Collecting (JOI19_ho_t4) C++17
100 / 100
78 ms 5796 KB
/*
 * Order of moving coins doesn't matter
 *
 * Manhattan distance always optimal?
 * - Assume we've assigned each coin to their destination.
 * - Then we move all by Manhattan distance
 * - start by assigning every coin to their closest destination
 *   - assign N coins to N positions to minimize sum of manhattan distances
 *   - solve for X and Y independently
 *      - for 1 row version they only need to be on diff X's
 *      - moves for Y is fixed?
 *          - sum of a_y - 1
 *      - in 2 row version
 *          - exactly 2 of each X
 *          - N "top" and N "bottom"
 *          - Y would be sum of abs(Y_i - 2) for tops + abs(Y_i - 1) for bottoms
 *              - assign greedily
 *              - given N values, find min sum of N of them abs(x - 2) + abs(x - 1)
 *                  - assign as many <= 1 as possible to abs(x - 1)
 *                  - assign as many > 1 as possible to abs(x - 2)
 *                  - move the remaining to wherever needed?
 *          - once we assign them to their Y's, we can break them up into two instances of 1 row problem
 *              - fairly convinced this is true?
 *              - how to solve 1d problem
 *                  - assign leftmost to leftmost, second to second, etc
 *      - for 2 row version we can do greedy over Y's
 * - then any moves to a different destination still follows Manhattan
 *   - nvm its not true
 *      - wtf its true
 *      - kekw
 *   - this constrains the problem to the box
 *   - so like just do some greedy in it i guess
 */

#include <bits/stdc++.h> 
using namespace std;

using ll = long long;

using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

const int MX = 200005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;

int num[2][MX];

int main(){
#ifdef mikey 
    freopen("a.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n; cin >> n;
    ll ans = 0;
    for (int i = 0; i < 2 * n; i++) {
        ll x, y; cin >> x >> y;
        ll go = x - 1;
        if (go < 0) ans += abs(go), go = 0;
        if (go >= n) ans += go - n + 1, go = n - 1;
        if (y <= 1) {
            ans += abs(y - 1);
            num[0][go]++;
        } else {
            ans += abs(y - 2);
            num[1][go]++;
        }
    }
    array<int, 2> pt;
    pt[0] = 0, pt[1] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 2; j++) {
            int &need = pt[j];    
            while (need < i && num[j][i] > 1) {
                if (num[j][need] == 0) {
                    num[j][i]--;
                    num[j][need]++;
                    // dbg("MOVE", i, j, need, j);
                    ans += abs(i - need);
                }
                ++need;
            }
        }
        for (int j = 0; j < 2; j++) {
            int &need = pt[j ^ 1];
            while (need < i && num[j][i] > 1) {
                if (num[j ^ 1][need] == 0) {
                    num[j][i]--;
                    num[j ^ 1][need]++;
                    // dbg("MOVE", i, j, need, j ^ 1);
                    ans += 1 + abs(i - need);
                }
                ++need;
            }
            if (num[j][i] > 1 && num[j ^ 1][i] == 0) {
                ++ans;
                num[j][i]--;
                num[j ^ 1][i]++;
                // dbg("MOVE", i, j, i, j ^ 1);
            }
            if (num[j][i] > 1 && i + 1 < n) {
                num[j][i + 1] += (num[j][i] - 1);
                ans += num[j][i] - 1;
                num[j][i] = 1;
                // dbg("MOVE", i, j, i + 1, j);
            }
        }
    }
    for (int j = 0; j < 2; j++)
    for (int i = 0; i < n; i++) {
        assert(num[j][i] == 1);
    }
    cout << ans << '\n';
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 1 ms 364 KB Output is correct
54 Correct 1 ms 364 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 1 ms 364 KB Output is correct
57 Correct 1 ms 364 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 364 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 2 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 1 ms 364 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 364 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 1 ms 364 KB Output is correct
54 Correct 1 ms 364 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 1 ms 364 KB Output is correct
57 Correct 1 ms 364 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1 ms 364 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 2 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 36 ms 2668 KB Output is correct
66 Correct 78 ms 5796 KB Output is correct
67 Correct 61 ms 4332 KB Output is correct
68 Correct 54 ms 4588 KB Output is correct
69 Correct 63 ms 4332 KB Output is correct
70 Correct 52 ms 4332 KB Output is correct
71 Correct 48 ms 4332 KB Output is correct
72 Correct 59 ms 4332 KB Output is correct
73 Correct 46 ms 4332 KB Output is correct
74 Correct 48 ms 4332 KB Output is correct
75 Correct 47 ms 4204 KB Output is correct
76 Correct 46 ms 4332 KB Output is correct
77 Correct 47 ms 4332 KB Output is correct
78 Correct 47 ms 4332 KB Output is correct
79 Correct 47 ms 4332 KB Output is correct
80 Correct 47 ms 4332 KB Output is correct
81 Correct 46 ms 4332 KB Output is correct
82 Correct 53 ms 4352 KB Output is correct
83 Correct 52 ms 4332 KB Output is correct
84 Correct 47 ms 4332 KB Output is correct
85 Correct 48 ms 4332 KB Output is correct
86 Correct 48 ms 4332 KB Output is correct
87 Correct 49 ms 4332 KB Output is correct
88 Correct 51 ms 4332 KB Output is correct
89 Correct 48 ms 4332 KB Output is correct
90 Correct 50 ms 4332 KB Output is correct
91 Correct 51 ms 4332 KB Output is correct
92 Correct 48 ms 4460 KB Output is correct
93 Correct 48 ms 4460 KB Output is correct
94 Correct 52 ms 4332 KB Output is correct
95 Correct 46 ms 4204 KB Output is correct
96 Correct 47 ms 4332 KB Output is correct
97 Correct 47 ms 4332 KB Output is correct
98 Correct 47 ms 4332 KB Output is correct
99 Correct 49 ms 4332 KB Output is correct
100 Correct 47 ms 4332 KB Output is correct
101 Correct 46 ms 4332 KB Output is correct
102 Correct 46 ms 4332 KB Output is correct
103 Correct 47 ms 4332 KB Output is correct