Submission #1084722

# Submission time Handle Problem Language Result Execution time Memory
1084722 2024-09-06T18:37:42 Z TrinhKhanhDung Drzava (COCI15_drzava) C++14
8 / 160
14 ms 4084 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAXN = 5e4 + 10, MAXK = 35;

int N, K;

struct city{
    ll x, y, c;
    city(ll _x = 0, ll _y = 0, ll _c = 0){
        x = _x;
        y = _y;
        c = _c;
    }
};
vector<city> cities;
vector<city> edges;

ll sqr(ll n){return n * n;}

ll dist(pair<ll, ll> a, pair<ll, ll> b){
    return sqr(a.fi - b.fi) + sqr(a.se - b.se);
}

set<int> mrge(set<int> &a, set<int> &b){
    set<int> ans;
    for(int i: a) for(int j: b){
        ans.insert((i + j) % K);
    }
    for(int i: a) ans.insert(i);
    for(int i: b) ans.insert(i);
    return ans;
}

struct DSU{
    vector<int> par, Sz;
    vector<set<int>> sets;

    DSU(int n = 0){
        par.resize(n + 3);
        Sz.resize(n + 3);
        sets.resize(n + 3);
        for(int i=0; i<=n; i++){
            par[i] = i;
            Sz[i] = 1;
        }
    }

    int root(int u){
        return u == par[u] ? u : par[u] = root(par[u]);
    }

    bool join(int a, int b){
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(Sz[a] < Sz[b]) swap(a, b);
        Sz[a] += Sz[b];
        par[b] = a;
        sets[a] = mrge(sets[a], sets[b]);
        return true;
    }
};

void solve(){
    cin >> N >> K;
    for(int i=1; i<=N; i++){
        int x, y, c;
        cin >> x >> y >> c;
        cities.push_back(city(x, y, c));
        if(c % K == 0){
            cout << "0.000";
            return;
        }
    }
    sort(ALL(cities), [&](city a, city b){return a.x < b.x;});
    ll P = oo;
    int j = 0;
    set<pair<ll, ll>> sets;
    for(int i=0; i<sz(cities); i++){
        ll x = cities[i].x, y = cities[i].y, c = cities[i].c;
        while(j < i && x - cities[j].x > P){
            sets.erase(make_pair(cities[j].y, j));
        }
        auto itL = sets.lower_bound(make_pair(x - P, 0));
        auto itR = sets.upper_bound(make_pair(x + P, oo));
        vector<pair<ll, ll>> ord;
        while(itL != itR){
            ord.push_back(*itL);
            itL++;
        }
        sort(ALL(ord), [&](pair<ll, ll> a, pair<ll, ll> b){
            ll xa = cities[a.se].x, ya = a.fi;
            ll xb = cities[b.se].y, yb = b.fi;
            return dist(make_pair(xa, ya), make_pair(x, y)) < dist(make_pair(xb, yb), make_pair(x, y));
        });
        for(int k=0; k<min(K, sz(ord)); k++){
            ll nx = cities[ord[k].se].x, ny = ord[k].fi;
            edges.push_back(city(i, ord[k].se, dist(make_pair(x, y), make_pair(nx, ny))));
        }
        if(sz(ord) >= K){
            ll nx = cities[ord[K - 1].se].x, ny = ord[K - 1].fi;
            minimize(P, dist(make_pair(nx, ny), make_pair(x, y)));
        }
        sets.insert(make_pair(y, i));
    }
    sort(ALL(edges), [&](city a, city b){
        return a.c < b.c;
    });
    DSU dsu(N);
    for(int i=0; i<N; i++){
        dsu.sets[i].insert(cities[i].c % K);
    }
    for(auto o: edges){
//        cout << o.x << ' ' << o.y << ' ' << o.c << '\n';
        o.x = dsu.root(o.x);
        o.y = dsu.root(o.y);
        if(o.x != o.y){
            for(int i: dsu.sets[o.x]) for(int j: dsu.sets[o.y]){
                if((i + j) % K == 0){
                    cout << fixed << setprecision(3) << sqrtl(o.c) << '\n';
                    return;
                }
            }
           dsu.join(o.x, o.y);
        }
//        for(int i: dsu.sets[o.x]) cout << i << ' ';
//        cout << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    // freopen("task.inp", "r", stdin);
    // freopen("task.out", "w", stdout);

    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}

Compilation message

drzava.cpp: In function 'void solve()':
drzava.cpp:107:46: warning: unused variable 'c' [-Wunused-variable]
  107 |         ll x = cities[i].x, y = cities[i].y, c = cities[i].c;
      |                                              ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 1 ms 760 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Runtime error 2 ms 600 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 512 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 6 ms 2516 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 452 KB Output is correct
2 Runtime error 14 ms 4008 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 13 ms 3796 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Runtime error 14 ms 4048 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 13 ms 4084 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 13 ms 3912 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 13 ms 3904 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 13 ms 4084 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 13 ms 4044 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -