Submission #600069

# Submission time Handle Problem Language Result Execution time Memory
600069 2022-07-20T12:34:04 Z Valaki2 Fountain Parks (IOI21_parks) C++17
35 / 100
1119 ms 155072 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;

#define x X
#define y Y

#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 2e5;
const vector<pii > directions = {mp(-2, 0), mp(0, -2), mp(0, 2), mp(2, 0)};

bool check_possibility(int n, vector<int> x, vector<int> y) {
    unordered_map<int, unordered_map<int, bool> > m;
    unordered_map<int, unordered_map<int, int> > f;
    for(int i = 0; i < n; i++) {
        f[x[i]][y[i]] = i + 1;
    }
    queue<int> q;
    q.push(0);
    m[x[0]][y[0]] = true;
    int cnt = 0;
    while(!q.empty()) {
        int cur = q.front();
        q.pop();
        cnt++;
        for(pii dir : directions) {
            pii new_point = mp(x[cur] + dir.fi, y[cur] + dir.se);
            if(f[new_point.fi][new_point.se] && !m[new_point.fi][new_point.se]) {
                m[new_point.fi][new_point.se] = true;
                int new_id = f[new_point.fi][new_point.se] - 1;
                q.push(new_id);
            }
        }
    }
    return (cnt == n);
}

struct fountain {
    int x, y, id;
    fountain() : x(0), y(0), id(-1) {}
    fountain(int x_, int y_, int id_) :
        x(x_), y(y_), id(id_) {}
};

int n;
vector<fountain > points;
vector<int> x;
vector<int> y;
unordered_map<int, unordered_map<int, int> > fountain_at;

unordered_map<int, unordered_map<int, bool> > bench_at;
unordered_map<int, unordered_map<int, bool> > road_at;
vector<int> ans_u;
vector<int> ans_v;
vector<int> ans_a;
vector<int> ans_b;

void build_road_bench(int road_u, int road_v, pii bench_pos) {
    ans_u.pb(road_u);
    ans_v.pb(road_v);
    ans_a.pb(bench_pos.fi);
    ans_b.pb(bench_pos.se);
    bench_at[bench_pos.fi][bench_pos.se] = true;
    road_at[(points[road_u].x + points[road_v].x) / 2][(points[road_u].y + points[road_v].y) / 2] = true;
}

vector<pii> get_bench_locations_pii(pii a, pii b) {
    fountain f_a = fountain(a.fi, a.se, -1), f_b = fountain(b.fi, b.se, -1);
    if(f_a.x == f_b.x) {
        int new_y = (f_a.y + f_b.y) / 2;
        a = mp(f_a.x + 1, new_y), b = mp(f_a.x - 1, new_y);
    } else {
        int new_x = (f_a.x + f_b.x) / 2;
        a = mp(new_x, f_a.y + 1), b = mp(new_x, f_a.y - 1);
    }
    return {min(a, b), max(a, b)};
}

vector<pii> get_bench_locations(int a_idx, int b_idx) {
    fountain f_a = points[a_idx], f_b = points[b_idx];
    pii a, b;
    if(f_a.x == f_b.x) {
        int new_y = (f_a.y + f_b.y) / 2;
        a = mp(f_a.x + 1, new_y), b = mp(f_a.x - 1, new_y);
    } else {
        int new_x = (f_a.x + f_b.x) / 2;
        a = mp(new_x, f_a.y + 1), b = mp(new_x, f_a.y - 1);
    }
    return {min(a, b), max(a, b)};
}

int get_middle_of_intersection(int a_idx, int b_idx) {
    pii a = mp(points[a_idx].x, points[a_idx].y);
    pii b = mp(points[b_idx].x, points[b_idx].y);
    int cnt = 0;
    for(pii dir : directions) {
        pii nei = mp(a.fi + dir.fi, a.se + dir.se);
        if(fountain_at[nei.fi][nei.se]) {
            cnt++;
        }
    }
    if(cnt == 4) {
        return a_idx;
    }
    cnt = 0;
    for(pii dir : directions) {
        pii nei = mp(b.fi + dir.fi, b.se + dir.se);
        if(fountain_at[nei.fi][nei.se]) {
            cnt++;
        }
    }
    if(cnt == 4) {
        return b_idx;
    }
    return -1;
}

pii get_bench_when_in_intersection(int a_idx, int b_idx) {
    // a is the middle, b is a neighbour
    fountain a = points[a_idx], b = points[b_idx];
    if(b.x < a.x) {
        return mp(a.x - 1, a.y + 1);
    }
    if(b.y < a.y) {
        return mp(a.x - 1, a.y - 1);
    }
    if(b.y > a.y) {
        return mp(a.x + 1, a.y + 1);
    }
    if(b.x > a.x) {
        return mp(a.x + 1, a.y - 1);
    }
}

bool bad_bench(pii bench, pii a, pii b) {
    if(a > b) {
        swap(a, b);
    }
    vector<pii > v = {a, b};
    for(pii dir : directions) {
        pii other = mp(bench.fi + dir.fi, bench.se + dir.se);
        vector<pii> corners = get_bench_locations_pii(bench, other);
        if(corners != v) {
            if(fountain_at[corners[0].fi][corners[0].se] &&
                fountain_at[corners[1].fi][corners[1].se] &&
                bench_at[other.fi][other.se] &&
                !road_at[(bench.fi + other.fi) / 2][(bench.se + other.se) / 2]) {
                return true;
            }
        }
    }
    return false;
}

void solve() {
    unordered_map<int, unordered_map<int, bool> > vis;
    queue<int> q;
    q.push(0);
    vis[x[0]][y[0]] = true;
    while(!q.empty()) {
        int cur = q.front();
        q.pop();
        for(pii dir : directions) {
            pii new_point = mp(x[cur] + dir.fi, y[cur] + dir.se);
            if(fountain_at[new_point.fi][new_point.se] && !vis[new_point.fi][new_point.se]) {
                vis[new_point.fi][new_point.se] = true;
                int new_id = fountain_at[new_point.fi][new_point.se] - 1;
                q.push(new_id);
                int mid = get_middle_of_intersection(cur, new_id);
                if(mid != -1) {
                    build_road_bench(cur, new_id, get_bench_when_in_intersection(mid, cur ^ new_id ^ mid));
                } else {
                    vector<pii> benches = get_bench_locations(cur, new_id);
                    if(!bench_at[benches[0].fi][benches[0].se] && !bad_bench(benches[0], mp(points[cur].x, points[cur].y), mp(points[new_id].x, points[new_id].y))) {
                        build_road_bench(cur, new_id, benches[0]);
                    } else {
                        build_road_bench(cur, new_id, benches[1]);
                    }
                }
            }
        }
    }
    //
}

#undef x
#undef y
int construct_roads(vector<int> x, vector<int> y) {
    // edge case
    if (x.size() == 1) {
        build({}, {}, {}, {});
        return 1;
    }
    // sample solution
    /*vector<int> u, v, a, b;
    u.push_back(0);
    v.push_back(1);
    a.push_back(x[0]+1);
    b.push_back(y[0]-1);
    build(u, v, a, b);*/
    n = x.size();
    if(!check_possibility(n, x, y)) {
        return 0;
    }
    X = x;
    Y = y;
    points.assign(n, fountain());
    for(int i = 0; i < n; i++) {
        points[i] = fountain(x[i], y[i], i);
        fountain_at[x[i]][y[i]] = i + 1;
    }
    solve();
    // build solution
    build(ans_u, ans_v, ans_a, ans_b);
    return 1;
}

/*
5
4 4
4 6
6 4
4 2
2 4
*/

Compilation message

parks.cpp: In function 'std::pair<int, int> get_bench_when_in_intersection(int, int)':
parks.cpp:138:1: warning: control reaches end of non-void function [-Wreturn-type]
  138 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 243 ms 37132 KB Output is correct
10 Correct 17 ms 3704 KB Output is correct
11 Correct 100 ms 19280 KB Output is correct
12 Correct 26 ms 5616 KB Output is correct
13 Correct 22 ms 7576 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 238 ms 37320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 243 ms 37132 KB Output is correct
10 Correct 17 ms 3704 KB Output is correct
11 Correct 100 ms 19280 KB Output is correct
12 Correct 26 ms 5616 KB Output is correct
13 Correct 22 ms 7576 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 238 ms 37320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 1 ms 304 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 653 ms 70796 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 4 ms 724 KB Output is correct
26 Correct 2 ms 568 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 192 ms 25428 KB Output is correct
29 Correct 331 ms 39916 KB Output is correct
30 Correct 406 ms 51012 KB Output is correct
31 Correct 666 ms 69872 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 296 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 0 ms 300 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 2 ms 572 KB Output is correct
45 Correct 322 ms 32996 KB Output is correct
46 Correct 527 ms 48032 KB Output is correct
47 Correct 551 ms 48200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 243 ms 37132 KB Output is correct
10 Correct 17 ms 3704 KB Output is correct
11 Correct 100 ms 19280 KB Output is correct
12 Correct 26 ms 5616 KB Output is correct
13 Correct 22 ms 7576 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 238 ms 37320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 1 ms 304 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 653 ms 70796 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 4 ms 724 KB Output is correct
26 Correct 2 ms 568 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 192 ms 25428 KB Output is correct
29 Correct 331 ms 39916 KB Output is correct
30 Correct 406 ms 51012 KB Output is correct
31 Correct 666 ms 69872 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 296 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 0 ms 300 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 2 ms 572 KB Output is correct
45 Correct 322 ms 32996 KB Output is correct
46 Correct 527 ms 48032 KB Output is correct
47 Correct 551 ms 48200 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 256 KB Output is correct
51 Incorrect 0 ms 212 KB Tree @(5, 5) appears more than once: for edges on positions 1 and 4
52 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 243 ms 37132 KB Output is correct
10 Correct 17 ms 3704 KB Output is correct
11 Correct 100 ms 19280 KB Output is correct
12 Correct 26 ms 5616 KB Output is correct
13 Correct 22 ms 7576 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 238 ms 37320 KB Output is correct
17 Correct 0 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 1054 ms 154584 KB Output is correct
21 Correct 938 ms 104340 KB Output is correct
22 Correct 972 ms 104332 KB Output is correct
23 Correct 767 ms 94000 KB Output is correct
24 Correct 85 ms 14624 KB Output is correct
25 Correct 69 ms 14660 KB Output is correct
26 Correct 69 ms 14656 KB Output is correct
27 Correct 802 ms 61616 KB Output is correct
28 Correct 844 ms 61692 KB Output is correct
29 Correct 939 ms 63520 KB Output is correct
30 Correct 941 ms 63580 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 37 ms 5156 KB Output is correct
33 Correct 90 ms 27164 KB Output is correct
34 Correct 1119 ms 155072 KB Output is correct
35 Correct 6 ms 1364 KB Output is correct
36 Correct 34 ms 5124 KB Output is correct
37 Correct 55 ms 9280 KB Output is correct
38 Correct 303 ms 25588 KB Output is correct
39 Correct 491 ms 35004 KB Output is correct
40 Correct 675 ms 45192 KB Output is correct
41 Correct 854 ms 54924 KB Output is correct
42 Correct 987 ms 64012 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 296 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 300 KB Output is correct
48 Correct 0 ms 296 KB Output is correct
49 Correct 1 ms 300 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 1 ms 300 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 296 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 2 ms 596 KB Output is correct
56 Correct 335 ms 32940 KB Output is correct
57 Correct 574 ms 48164 KB Output is correct
58 Correct 577 ms 48256 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 579 ms 69892 KB Output is correct
63 Correct 559 ms 69944 KB Output is correct
64 Correct 541 ms 69584 KB Output is correct
65 Correct 3 ms 724 KB Output is correct
66 Correct 5 ms 1068 KB Output is correct
67 Correct 375 ms 32588 KB Output is correct
68 Correct 584 ms 48824 KB Output is correct
69 Correct 788 ms 65496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 243 ms 37132 KB Output is correct
10 Correct 17 ms 3704 KB Output is correct
11 Correct 100 ms 19280 KB Output is correct
12 Correct 26 ms 5616 KB Output is correct
13 Correct 22 ms 7576 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 238 ms 37320 KB Output is correct
17 Correct 874 ms 107916 KB Output is correct
18 Correct 975 ms 108112 KB Output is correct
19 Correct 1020 ms 104240 KB Output is correct
20 Correct 667 ms 55008 KB Output is correct
21 Correct 826 ms 60548 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Incorrect 83 ms 10876 KB Tree @(186063, 21279) appears more than once: for edges on positions 74 and 88
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 243 ms 37132 KB Output is correct
10 Correct 17 ms 3704 KB Output is correct
11 Correct 100 ms 19280 KB Output is correct
12 Correct 26 ms 5616 KB Output is correct
13 Correct 22 ms 7576 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 452 KB Output is correct
16 Correct 238 ms 37320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 1 ms 304 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 653 ms 70796 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 4 ms 724 KB Output is correct
26 Correct 2 ms 568 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 192 ms 25428 KB Output is correct
29 Correct 331 ms 39916 KB Output is correct
30 Correct 406 ms 51012 KB Output is correct
31 Correct 666 ms 69872 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 296 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 0 ms 300 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 2 ms 572 KB Output is correct
45 Correct 322 ms 32996 KB Output is correct
46 Correct 527 ms 48032 KB Output is correct
47 Correct 551 ms 48200 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 256 KB Output is correct
51 Incorrect 0 ms 212 KB Tree @(5, 5) appears more than once: for edges on positions 1 and 4
52 Halted 0 ms 0 KB -