Submission #584633

# Submission time Handle Problem Language Result Execution time Memory
584633 2022-06-27T17:51:37 Z MilosMilutinovic Drzava (COCI15_drzava) C++14
72 / 160
1000 ms 55756 KB
/**
 *    author:  wxhtzdy
 *    created: 27.06.2022 18:47:13
**/
#include <bits/stdc++.h>

using namespace std;

struct point {
  int x, y, c;

  bool operator < (const point &p) const {
    return x < p.x || (x == p.x && y < p.y);
  };
};

long long Dist(point a, point b) {
  long long dx = abs(a.x - b.x);
  long long dy = abs(a.y - b.y);
  return dx * dx + dy * dy;
}

point a[50000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, k;
  cin >> n >> k;     
  for (int i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y >> a[i].c;
    a[i].c %= k;
  }
  sort(a, a + n);
  if (n <= 1000) {
    vector<tuple<double, int, int>> edges;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j) {
          continue;
        }
        edges.push_back(make_tuple(sqrt(Dist(a[i], a[j])), i, j));
      }
    }
    sort(edges.begin(), edges.end());
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    function<int(int)> root = [&](int x) {
      return p[x] == x ? x : (p[x] = root(p[x]));
    };
    vector<vector<int>> ch(n);
    for (int i = 0; i < n; i++) {
      ch[i].push_back(i);
    }
    auto Unite = [&](int x, int y) {
      x = root(x);
      y = root(y);
      if (x == y) {
        return false;
      }        
      p[y] = x;
      for (int i : ch[y]) {
        ch[x].push_back(i);
      }                             
      ch[y].clear();
      vector<bool> dp(k + 1);
      dp[0] = true;
      for (int i : ch[x]) {
        vector<bool> new_dp(k + 1);
        for (int p = k; p >= 0; p--) {
          new_dp[p] = (dp[p] | dp[(((p - a[i].c) % k) + k) % k]);
        }
        swap(new_dp, dp);
        if (dp[k]) {
          return true;
        }
      }
      return false;
    };
    for (int i = 0; i < (int) edges.size(); i++) {
      if (Unite(get<1>(edges[i]), get<2>(edges[i]))) {
        cout << fixed << setprecision(3) << get<0>(edges[i]) << '\n';
        return 0;
      }
    }       
    assert(false);
    return 0;
  }
  auto Check = [&](long long x) {
    int d = (double) (sqrt(x) + 1);
    vector<vector<int>> g(n);
    auto Add = [&](int x, int y) {
      g[x].push_back(y);
      g[y].push_back(x);
    };
    set<pair<int, int>> s;
    int ptr = 0;
    for (int i = 0; i < n; i++) {
      while (ptr < i && a[ptr].x + d < a[i].x) {
        s.erase(s.find(make_pair(a[ptr].y, ptr)));  
        ptr += 1;
      }               
      auto it = s.lower_bound(make_pair(a[i].y - d, -1));
      int cnt = 0;
      while (it != s.end()) {
        int j = it->second;
        if (abs(a[i].y - a[j].y) > d) {
          break;
        }
        cnt += 1;
        if (cnt >= 8 * k) {
          return true;
        }  
        if (Dist(a[i], a[j]) <= x) {
          Add(i, j);
        }
        it = next(it);   
      }
      s.emplace(a[i].y, i);
    }                    
    vector<bool> was(n);
    for (int i = 0; i < n; i++) {
      if (!was[i]) {
        vector<int> que(1, i);
        was[i] = true;
        for (int b = 0; b < (int) que.size(); b++) {
          int j = que[b];
          for (int p : g[j]) {
            if (!was[p]) {
              was[p] = true;
              que.push_back(p);
            }
          }
        }
        vector<bool> dp(k + 1, false);
        dp[0] = true;
        for (int j : que) {
          vector<bool> new_dp(k + 1);
          for (int p = k; p >= 0; p--) {
            new_dp[p] = (dp[p] | dp[(((p - a[i].c) % k) + k) % k]);
          }
          swap(new_dp, dp);
          if (dp[k]) {
            return true;
          }
        }
      }
    }
    return false;
  };
  long long low = 0, high = 1e18, ans = 0;
  while (low <= high) {
    long long mid = low + high >> 1;
    if (Check(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << fixed << setprecision(3) << sqrt(ans * 1.0) << '\n';
  return 0;
}

Compilation message

drzava.cpp: In lambda function:
drzava.cpp:137:18: warning: unused variable 'j' [-Wunused-variable]
  137 |         for (int j : que) {
      |                  ^
drzava.cpp: In function 'int main()':
drzava.cpp:153:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  153 |     long long mid = low + high >> 1;
      |                     ~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 124 ms 16832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 115 ms 16844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 48 ms 8660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 116 ms 16836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 154 ms 16824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 41 ms 8648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 224 ms 5152 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 1074 ms 51680 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Execution timed out 1089 ms 50932 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 837 ms 26544 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 818 ms 30496 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 1068 ms 55756 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 328 KB Output is correct
2 Correct 428 ms 7336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 570 ms 18984 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 564 ms 21844 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 748 ms 22012 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 561 ms 12856 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 612 ms 8996 KB Output isn't correct
3 Halted 0 ms 0 KB -