Submission #1058789

# Submission time Handle Problem Language Result Execution time Memory
1058789 2024-08-14T13:51:32 Z LittleOrange Fountain Parks (IOI21_parks) C++17
55 / 100
2856 ms 74676 KB
#include "parks.h"
#include<bits/stdc++.h>
using namespace std;
using ll = int;
struct dsu{
    ll n;
    vector<ll> p;
    dsu(ll N):n(N),p(N,-1){}
    ll g(ll i){
        return p[i]<0?i:p[i]=g(p[i]);
    }
    bool m(ll a, ll b){
        a = g(a);
        b = g(b);
        if(a==b) return false;
        if(p[a]>p[b]) swap(a,b);
        p[a]+=p[b];
        p[b]=a;
        return true;
    }
};
struct obj{
    ll x,y,i;
    bool operator<(const obj &o) const{
        return x!=o.x?x<o.x:y<o.y;
    }
    obj operator+(const obj &o) const{
        return {x+o.x,y+o.y,i};
    }
    obj operator-(const obj &o) const{
        return {x-o.x,y-o.y,i};
    }
    bool operator==(const obj &o) const{
        return x==o.x&&y==o.y;
    }
    bool operator!=(const obj &o) const{
        return x!=o.x||y!=o.y;
    }
    obj turn() const{
        return {y,-x,i};
    }
};
struct pr{
    ll i,c;
    bool operator<(const pr &o) const{
        return c>o.c;
    }
};
using pos = obj;
ostream& operator<<(ostream& o, const pos &p){
    return o << "(" << p.x << "," << p.y << ";" << p.i << ")";
}
pos mid(const pos &a, const pos &b){
    return {(a.x+b.x)/2,(a.y+b.y)/2,0};
}
array<pos,2> mids(const pos &a, const pos &b){
    pos m = mid(a,b);
    array<pos,2> r = {m+(a-m).turn(),m+(b-m).turn()};
    ////cerr << "mids of " << a << " " << b << " -> " << r[0] << " " << r[1] << "\n";
    return r;
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
    mt19937_64 mt(random_device{}());
    ll n = x.size();
    vector<obj> fs = {{0,2,0},{0,-2,0},{2,0,0},{-2,0,0}};
    if (x.size() == 1) {
	    build({}, {}, {}, {});
        return 1;
    }
    ll cnt = 10;
    while(cnt--){
        vector<ll> ans_u,ans_v,ans_a,ans_b;
        vector<obj> a(n);
        for(ll i = 0;i<n;i++){
            a[i] = {x[i],y[i],i};
        }
        //shuffle(ord.begin(),ord.end(),mt);
        sort(a.begin(),a.end());
        dsu d(n);
        vector<pair<pos,pos>> cons;
        vector<pair<pos,pos>> ord;
        for(pos u : a){
            for(pos f : fs){
                pos v = u+f;
                auto it = lower_bound(a.begin(),a.end(),v);
                if (it==a.end()||*it!=v) continue;
                v = *it;
                ord.push_back({u,v});
            }
        }
        shuffle(ord.begin(),ord.end(),mt);
        for(auto [u,v] : ord){
            if(d.m(u.i,v.i)){
                //cerr << "con " << u << " " << v << "\n";
                cons.push_back({u,v});
            }
        }
        if(cons.size()<n-1) return 0;
        vector<pos> v;
        for(auto [p0,p1] : cons){
            for(pos p : mids(p0,p1)) v.push_back(p);
        }
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(),v.end()),v.end());
        ll m = v.size();
        vector<pair<pos,pos>> ops;
        vector<vector<ll>> con(m);
        vector<ll> u(n-1,0);
        for(auto [p0,p1] : cons){
            auto o = mids(p0,p1);
            ll i = lower_bound(v.begin(),v.end(),o[0])-v.begin();
            ll j = lower_bound(v.begin(),v.end(),o[1])-v.begin();
            pair<pos,pos> op = {min(o[0],o[1]),max(o[0],o[1])};
            //cerr << "ops " << op.first << " " << op.second << "\n";
            ops.push_back(op);
            con[i].push_back(j);
            con[j].push_back(i);
        }
        sort(ops.begin(),ops.end());
        vector<ll> um(m,0);
        //shuffle(idl.begin(),idl.end(),mt);
        priority_queue<pr> q;
        for(ll i = 0;i<m;i++) q.push({i,con[i].size()});
        while(!q.empty()){
            auto [i,_] = q.top();q.pop();
            shuffle(con[i].begin(),con[i].end(),mt);
            for(ll j : con[i]){
                pair<pos,pos> op = {min(v[i],v[j]),max(v[i],v[j])};
                //cerr << "test " << op.first << " " << op.second << "\n";
                auto it = lower_bound(ops.begin(),ops.end(),op);
                if (it==ops.end()) {
                    //cerr << "error 1\n";
                    continue;
                }
                //cerr << "got " << (*it).first << " " << (*it).second << "\n";
                if ((*it)!=op) {
                    //cerr << "error 2\n";
                    continue;
                }
                ll idx = it-ops.begin();
                //cerr << "idx= " << idx << "\n";
                if(u[idx]) {
                    //cerr << "error 3\n";
                    continue;
                }
                u[idx] = 1;
                um[i] = 1;
                for(ll z = 0;z<con[j].size();z++) if(con[j][z]==i){
                    con[j].erase(con[j].begin()+z);
                    break;
                }
                q.push({j,con[j].size()});
                pos p = v[i];
                ans_a.push_back(p.x);
                ans_b.push_back(p.y);
                auto os = mids(v[i],v[j]);
                //cerr << "use " << p << " from " << os[0] << " " << os[1] << "\n";
                ll idx1 = (*lower_bound(a.begin(),a.end(),os[0])).i;
                ll idx2 = (*lower_bound(a.begin(),a.end(),os[1])).i;
                ans_u.push_back(idx1);
                ans_v.push_back(idx2);
                break;
            }
        }
        if(ans_u.size()<n-1) continue;
        build(ans_u,ans_v,ans_a,ans_b);
        return 1;
    }
    return 0;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:98:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<obj, obj> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
   98 |         if(cons.size()<n-1) return 0;
      |            ~~~~~~~~~~~^~~~
parks.cpp:123:52: warning: narrowing conversion of '(& con.std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)i)))->std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'll' {aka 'int'} [-Wnarrowing]
  123 |         for(ll i = 0;i<m;i++) q.push({i,con[i].size()});
      |                                         ~~~~~~~~~~~^~
parks.cpp:148:31: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |                 for(ll z = 0;z<con[j].size();z++) if(con[j][z]==i){
      |                              ~^~~~~~~~~~~~~~
parks.cpp:152:38: warning: narrowing conversion of '(& con.std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)j)))->std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'll' {aka 'int'} [-Wnarrowing]
  152 |                 q.push({j,con[j].size()});
      |                           ~~~~~~~~~~~^~
parks.cpp:165:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
  165 |         if(ans_u.size()<n-1) continue;
      |            ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 360 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 344 KB Output is correct
9 Correct 148 ms 34456 KB Output is correct
10 Correct 12 ms 3796 KB Output is correct
11 Correct 70 ms 18416 KB Output is correct
12 Correct 19 ms 5416 KB Output is correct
13 Correct 14 ms 7344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 149 ms 35464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 360 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 344 KB Output is correct
9 Correct 148 ms 34456 KB Output is correct
10 Correct 12 ms 3796 KB Output is correct
11 Correct 70 ms 18416 KB Output is correct
12 Correct 19 ms 5416 KB Output is correct
13 Correct 14 ms 7344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 149 ms 35464 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 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 357 ms 65448 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 2 ms 1052 KB Output is correct
27 Correct 2 ms 1008 KB Output is correct
28 Correct 123 ms 28096 KB Output is correct
29 Correct 182 ms 39580 KB Output is correct
30 Correct 274 ms 54200 KB Output is correct
31 Correct 329 ms 65020 KB Output is correct
32 Correct 0 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 0 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 424 KB Output is correct
39 Correct 0 ms 344 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 796 KB Output is correct
45 Correct 149 ms 32220 KB Output is correct
46 Correct 260 ms 50524 KB Output is correct
47 Correct 233 ms 46008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 360 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 344 KB Output is correct
9 Correct 148 ms 34456 KB Output is correct
10 Correct 12 ms 3796 KB Output is correct
11 Correct 70 ms 18416 KB Output is correct
12 Correct 19 ms 5416 KB Output is correct
13 Correct 14 ms 7344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 149 ms 35464 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 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 357 ms 65448 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 2 ms 1052 KB Output is correct
27 Correct 2 ms 1008 KB Output is correct
28 Correct 123 ms 28096 KB Output is correct
29 Correct 182 ms 39580 KB Output is correct
30 Correct 274 ms 54200 KB Output is correct
31 Correct 329 ms 65020 KB Output is correct
32 Correct 0 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 0 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 424 KB Output is correct
39 Correct 0 ms 344 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 796 KB Output is correct
45 Correct 149 ms 32220 KB Output is correct
46 Correct 260 ms 50524 KB Output is correct
47 Correct 233 ms 46008 KB Output is correct
48 Correct 0 ms 348 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 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 2856 ms 67056 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 360 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 344 KB Output is correct
9 Correct 148 ms 34456 KB Output is correct
10 Correct 12 ms 3796 KB Output is correct
11 Correct 70 ms 18416 KB Output is correct
12 Correct 19 ms 5416 KB Output is correct
13 Correct 14 ms 7344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 149 ms 35464 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 288 ms 55540 KB Output is correct
21 Correct 317 ms 57272 KB Output is correct
22 Correct 308 ms 59572 KB Output is correct
23 Correct 291 ms 67760 KB Output is correct
24 Correct 62 ms 8392 KB Output is correct
25 Correct 87 ms 29896 KB Output is correct
26 Correct 77 ms 29456 KB Output is correct
27 Correct 328 ms 72856 KB Output is correct
28 Correct 338 ms 74676 KB Output is correct
29 Correct 387 ms 74536 KB Output is correct
30 Correct 359 ms 69808 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 23 ms 4668 KB Output is correct
33 Correct 34 ms 4820 KB Output is correct
34 Correct 324 ms 60240 KB Output is correct
35 Correct 5 ms 1752 KB Output is correct
36 Correct 22 ms 8092 KB Output is correct
37 Correct 43 ms 15968 KB Output is correct
38 Correct 148 ms 27504 KB Output is correct
39 Correct 185 ms 32700 KB Output is correct
40 Correct 242 ms 45488 KB Output is correct
41 Correct 314 ms 59784 KB Output is correct
42 Correct 347 ms 65464 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 0 ms 436 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 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 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 796 KB Output is correct
56 Correct 153 ms 32936 KB Output is correct
57 Correct 232 ms 47564 KB Output is correct
58 Correct 244 ms 50612 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 339 ms 72820 KB Output is correct
63 Correct 353 ms 74420 KB Output is correct
64 Correct 357 ms 72160 KB Output is correct
65 Correct 2 ms 856 KB Output is correct
66 Correct 3 ms 1372 KB Output is correct
67 Correct 150 ms 31704 KB Output is correct
68 Correct 259 ms 53432 KB Output is correct
69 Correct 344 ms 66740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 360 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 344 KB Output is correct
9 Correct 148 ms 34456 KB Output is correct
10 Correct 12 ms 3796 KB Output is correct
11 Correct 70 ms 18416 KB Output is correct
12 Correct 19 ms 5416 KB Output is correct
13 Correct 14 ms 7344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 149 ms 35464 KB Output is correct
17 Correct 353 ms 73912 KB Output is correct
18 Correct 363 ms 69556 KB Output is correct
19 Correct 293 ms 55988 KB Output is correct
20 Correct 330 ms 64516 KB Output is correct
21 Correct 309 ms 64692 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 47 ms 10652 KB Output is correct
24 Correct 9 ms 3540 KB Output is correct
25 Correct 32 ms 13360 KB Output is correct
26 Correct 56 ms 18884 KB Output is correct
27 Correct 162 ms 31932 KB Output is correct
28 Correct 202 ms 40628 KB Output is correct
29 Correct 266 ms 52152 KB Output is correct
30 Correct 333 ms 61364 KB Output is correct
31 Correct 396 ms 66228 KB Output is correct
32 Correct 372 ms 68472 KB Output is correct
33 Correct 405 ms 70836 KB Output is correct
34 Correct 3 ms 1116 KB Output is correct
35 Correct 4 ms 1724 KB Output is correct
36 Correct 177 ms 32488 KB Output is correct
37 Correct 329 ms 49840 KB Output is correct
38 Correct 443 ms 63668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 360 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 344 KB Output is correct
9 Correct 148 ms 34456 KB Output is correct
10 Correct 12 ms 3796 KB Output is correct
11 Correct 70 ms 18416 KB Output is correct
12 Correct 19 ms 5416 KB Output is correct
13 Correct 14 ms 7344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 149 ms 35464 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 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 357 ms 65448 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 2 ms 1052 KB Output is correct
27 Correct 2 ms 1008 KB Output is correct
28 Correct 123 ms 28096 KB Output is correct
29 Correct 182 ms 39580 KB Output is correct
30 Correct 274 ms 54200 KB Output is correct
31 Correct 329 ms 65020 KB Output is correct
32 Correct 0 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 0 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 424 KB Output is correct
39 Correct 0 ms 344 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 796 KB Output is correct
45 Correct 149 ms 32220 KB Output is correct
46 Correct 260 ms 50524 KB Output is correct
47 Correct 233 ms 46008 KB Output is correct
48 Correct 0 ms 348 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 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 2856 ms 67056 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -