답안 #1053303

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1053303 2024-08-11T10:23:50 Z MercubytheFirst 분수 공원 (IOI21_parks) C++17
30 / 100
321 ms 79024 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a);
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p);
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s);
template<typename T, typename cmp>
std::ostream& operator<<(std::ostream& os, const std::set<T, cmp>& s);


struct Road
{
    pair<int, int> u, v, bench;
    int u_idx, v_idx, idx;
    int o;
    Road() {
        u = v = bench = {-1, -1};
        u_idx = v_idx = idx = o = -1;
    }
    Road(const pii& a, const pii& b, int a_idx, int b_idx, int j) : u(a), v(b), u_idx(a_idx), v_idx(b_idx), idx(j) {
        bench = {-1, -1};
        if(u > v) {
            swap(u, v);
            swap(u_idx, v_idx);
        }
        // assert(u.first == v.first or u.second == v.second);
        if(u.first == v.first) { // vertical
            o = 0;
        }
        else { // horizontal
            o = 1;
        }
    }
    bool operator<(const Road& other) const {
        return make_pair(u, v) < make_pair(other.u, other.v);
    }
    friend ostream& operator<<(ostream& os, const Road& r) {
        os << r.u_idx << " -> " << r.v_idx << " : " << r.u << " --> " << r.v;
        return os;
    }
};

int construct_roads(vector<int> x, vector<int> y) {
    int n = x.size();
    // vector<pii> points(n);
    // for(int i = 0; i < n; ++i) {
    //     points[i] = {x[i], y[i]};
    // }
    vector<bool> vis(n);
    map<pair<int, int>, int> mp;
    for(int i = 0; i < n; ++i) {
        mp[{x[i], y[i]}] = i;
    }
    const int dx[] = {0, +2, 0, -2},
              dy[] = {+2, 0, -2, 0};
    vector<Road> roads;
    auto road_dfs = [&](auto&& dfs, int i, int j) -> void
    {
        const int idx = mp[{i, j}];
        if(vis[idx])  {
            return;
        }
        vis[idx] = true;
        for(int d = 0; d < 4; ++d) {
            const int ii = i + dx[d], jj = j + dy[d];
            auto it = mp.find({ii, jj});
            if(it != mp.end() and !vis[it->second]) {
                roads.push_back(Road({i,j}, {ii,jj}, idx, it->second, roads.size()));
                dfs(dfs, ii, jj);
            }
        }
    };
    road_dfs(road_dfs, x[0], y[0]);
    // assert((int)roads.size() < n);
    if((int)roads.size() != n - 1) {
        return 0;
    }
    sort(roads.begin(), roads.end());
    map<pair<int, int>, int> taken;
    bool ok = true;
    for(int i = 0; i < (int)roads.size(); ++i) {
        const Road& r = roads[i];
        array<int, 2> co{(r.u.first + r.v.first)/2, (r.u.second + r.v.second) / 2};
        co[r.o] -= 1;
        if(!taken.count({co[0], co[1]})) {
            taken[{co[0], co[1]}] = true;
            roads[i].bench = {co[0], co[1]};
            continue;
        }
        co[r.o] += 2;
        if(!taken.count({co[0], co[1]})) {
            taken[{co[0], co[1]}] = true;
            roads[i].bench = {co[0], co[1]};
        }
        else {
            ok = false;
            break;
        }
        // else return 0;    // for(const auto& x : roads) {
    //     cout << x.u << " -> " << x.v << endl;
    // }
    }
    if(!ok) {
        roads.assign(n, Road());
        taken.clear();
        auto road_dfs2 = [&](auto&& dfs, int i, int j) -> void
        {
            const int idx = mp[{i, j}];
            if(vis[idx])  {
                return;
            }
            vis[idx] = true;
            for(int d = 0; d < 4; ++d) {
                const int ii = i + dx[d], jj = j + dy[d];
                auto it = mp.find({ii, jj});
                if(it != mp.end() and !vis[it->second]) {
                    roads.push_back(Road({i,j}, {ii,jj}, idx, it->second, roads.size()));
                    dfs(dfs, ii, jj);
                }
            }
        };
        road_dfs2(road_dfs2, x[0], y[0]);
        sort(roads.begin(), roads.end(),[&](const Road& a, const Road& b) { return make_pair(a.v, a.u) < make_pair(b.v, b.u); });
        for(int i = 0; i < (int)roads.size(); ++i) {
            const Road& r = roads[i];
            array<int, 2> co{(r.u.first + r.v.first)/2, (r.u.second + r.v.second) / 2};
            co[r.o] += 1;
            if(!taken.count({co[0], co[1]})) {
                taken[{co[0], co[1]}] = true;
                roads[i].bench = {co[0], co[1]};
                continue;
            }
            co[r.o] -= 2;
            if(!taken.count({co[0], co[1]})) {
                taken[{co[0], co[1]}] = true;
                roads[i].bench = {co[0], co[1]};
            }
            else {
                return 0;
            }
        }
    }
    const int m = roads.size();
    vector<int> ansu(m), ansv(m), ansa(m), ansb(m);
    for(int i = 0; i < m; ++i) {
        ansu[roads[i].idx] = roads[i].u_idx;
        ansv[roads[i].idx] = roads[i].v_idx;
        ansa[roads[i].idx] = roads[i].bench.first;
        ansb[roads[i].idx] = roads[i].bench.second;
    }
    build(ansu, ansv, ansa, ansb);
    return 1;
}




template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a) {
  os << "[";
  for(size_t i = 0; i + 1 < N; ++i) {
    os << a[i] << ", ";
  }
  if(N > 0)
    os << a[N - 1];
  os << "]";
  return os;
}
 
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
  os << "(" << p.first << ", " << p.second << ") ";
  return os;
}
 
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
  os << '[';
  for(auto x : v)
    os << x << ", ";
  os << "] ";
  return os;
}
 
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
  os << "{";
  for(auto x : s)
    os << x << ", ";
  os << "} ";
  return os;
}
//
template<typename T, typename cmp>
std::ostream& operator<<(std::ostream& os, const std::set<T, cmp>& s) {
  os << "{";
  for(auto x : s)
    os << x << ", ";
  os << "} ";
  return os;
}

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

6
4 4
4 6
6 4
4 2
2 4
2 6

2
2 2
4 6
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 115 ms 37444 KB Output is correct
10 Correct 9 ms 4176 KB Output is correct
11 Correct 54 ms 20984 KB Output is correct
12 Correct 14 ms 5972 KB Output is correct
13 Correct 25 ms 9936 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 105 ms 31172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 115 ms 37444 KB Output is correct
10 Correct 9 ms 4176 KB Output is correct
11 Correct 54 ms 20984 KB Output is correct
12 Correct 14 ms 5972 KB Output is correct
13 Correct 25 ms 9936 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 105 ms 31172 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 265 ms 78096 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 86 ms 32652 KB Output is correct
29 Correct 139 ms 46376 KB Output is correct
30 Correct 211 ms 63400 KB Output is correct
31 Correct 275 ms 79024 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 108 ms 33908 KB Output is correct
46 Correct 161 ms 48624 KB Output is correct
47 Correct 165 ms 49080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 115 ms 37444 KB Output is correct
10 Correct 9 ms 4176 KB Output is correct
11 Correct 54 ms 20984 KB Output is correct
12 Correct 14 ms 5972 KB Output is correct
13 Correct 25 ms 9936 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 105 ms 31172 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 265 ms 78096 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 86 ms 32652 KB Output is correct
29 Correct 139 ms 46376 KB Output is correct
30 Correct 211 ms 63400 KB Output is correct
31 Correct 275 ms 79024 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 108 ms 33908 KB Output is correct
46 Correct 161 ms 48624 KB Output is correct
47 Correct 165 ms 49080 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 285 ms 75252 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 1116 KB Output is correct
58 Correct 5 ms 2480 KB Output is correct
59 Correct 3 ms 1628 KB Output is correct
60 Correct 117 ms 38444 KB Output is correct
61 Correct 170 ms 51892 KB Output is correct
62 Correct 212 ms 62640 KB Output is correct
63 Correct 268 ms 74420 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 1 ms 344 KB Output is correct
67 Correct 254 ms 69944 KB Output is correct
68 Correct 243 ms 68528 KB Output is correct
69 Correct 248 ms 71848 KB Output is correct
70 Correct 2 ms 860 KB Output is correct
71 Correct 3 ms 1372 KB Output is correct
72 Correct 104 ms 28328 KB Output is correct
73 Correct 170 ms 46584 KB Output is correct
74 Correct 239 ms 56456 KB Output is correct
75 Correct 266 ms 71600 KB Output is correct
76 Correct 253 ms 76976 KB Output is correct
77 Correct 2 ms 1116 KB Output is correct
78 Correct 3 ms 1628 KB Output is correct
79 Correct 105 ms 29696 KB Output is correct
80 Correct 176 ms 44896 KB Output is correct
81 Correct 235 ms 60344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 115 ms 37444 KB Output is correct
10 Correct 9 ms 4176 KB Output is correct
11 Correct 54 ms 20984 KB Output is correct
12 Correct 14 ms 5972 KB Output is correct
13 Correct 25 ms 9936 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 105 ms 31172 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 278 ms 76496 KB Output is correct
21 Correct 270 ms 72112 KB Output is correct
22 Correct 301 ms 67504 KB Output is correct
23 Correct 201 ms 56588 KB Output is correct
24 Correct 80 ms 15972 KB Output is correct
25 Correct 79 ms 15956 KB Output is correct
26 Correct 79 ms 16024 KB Output is correct
27 Correct 233 ms 46256 KB Output is correct
28 Correct 235 ms 47024 KB Output is correct
29 Correct 321 ms 47536 KB Output is correct
30 Correct 300 ms 46256 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Incorrect 11 ms 3160 KB Solution announced impossible, but it is possible.
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 115 ms 37444 KB Output is correct
10 Correct 9 ms 4176 KB Output is correct
11 Correct 54 ms 20984 KB Output is correct
12 Correct 14 ms 5972 KB Output is correct
13 Correct 25 ms 9936 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 105 ms 31172 KB Output is correct
17 Correct 266 ms 78004 KB Output is correct
18 Correct 258 ms 66048 KB Output is correct
19 Incorrect 269 ms 68788 KB Solution announced impossible, but it is possible.
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 115 ms 37444 KB Output is correct
10 Correct 9 ms 4176 KB Output is correct
11 Correct 54 ms 20984 KB Output is correct
12 Correct 14 ms 5972 KB Output is correct
13 Correct 25 ms 9936 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 105 ms 31172 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 265 ms 78096 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 86 ms 32652 KB Output is correct
29 Correct 139 ms 46376 KB Output is correct
30 Correct 211 ms 63400 KB Output is correct
31 Correct 275 ms 79024 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 108 ms 33908 KB Output is correct
46 Correct 161 ms 48624 KB Output is correct
47 Correct 165 ms 49080 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 285 ms 75252 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 1116 KB Output is correct
58 Correct 5 ms 2480 KB Output is correct
59 Correct 3 ms 1628 KB Output is correct
60 Correct 117 ms 38444 KB Output is correct
61 Correct 170 ms 51892 KB Output is correct
62 Correct 212 ms 62640 KB Output is correct
63 Correct 268 ms 74420 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 1 ms 344 KB Output is correct
67 Correct 254 ms 69944 KB Output is correct
68 Correct 243 ms 68528 KB Output is correct
69 Correct 248 ms 71848 KB Output is correct
70 Correct 2 ms 860 KB Output is correct
71 Correct 3 ms 1372 KB Output is correct
72 Correct 104 ms 28328 KB Output is correct
73 Correct 170 ms 46584 KB Output is correct
74 Correct 239 ms 56456 KB Output is correct
75 Correct 266 ms 71600 KB Output is correct
76 Correct 253 ms 76976 KB Output is correct
77 Correct 2 ms 1116 KB Output is correct
78 Correct 3 ms 1628 KB Output is correct
79 Correct 105 ms 29696 KB Output is correct
80 Correct 176 ms 44896 KB Output is correct
81 Correct 235 ms 60344 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 278 ms 76496 KB Output is correct
86 Correct 270 ms 72112 KB Output is correct
87 Correct 301 ms 67504 KB Output is correct
88 Correct 201 ms 56588 KB Output is correct
89 Correct 80 ms 15972 KB Output is correct
90 Correct 79 ms 15956 KB Output is correct
91 Correct 79 ms 16024 KB Output is correct
92 Correct 233 ms 46256 KB Output is correct
93 Correct 235 ms 47024 KB Output is correct
94 Correct 321 ms 47536 KB Output is correct
95 Correct 300 ms 46256 KB Output is correct
96 Correct 0 ms 344 KB Output is correct
97 Incorrect 11 ms 3160 KB Solution announced impossible, but it is possible.
98 Halted 0 ms 0 KB -