comp-geometry

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Luzhiled/comp-geometry

:heavy_check_mark: test/aoj/icpc/1175.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1175

#include "src/real-geometry/class/circle.hpp"
#include "src/real-geometry/position/intersect-cc.hpp"

#include <iostream>
#include <vector>
#include <algorithm>

using CI = std::pair< geometry::circle<geometry::f80>, int >;
std::vector< int > dp;
std::vector< int > mask;

void precalc(const std::vector< CI > &cs) {
  int n = cs.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (geometry::intersect_cc(cs[i].first, cs[j].first) >= 3) continue;
      mask[i] |= (1 << j);
    }
  }
}

bool can_remove(int idx, int bit) {
  return (mask[idx] & bit) == mask[idx];
}

int calc(int bit, int cnt, const std::vector< CI > &cs) {
  if (dp[bit] != -1) return dp[bit];

  int n = cs.size();
  int &res = dp[bit];
  res = cnt;

  for (int i = 0; i < n; i++) {
    if (bit & (1 << i)) continue;
    if (can_remove(i, bit) == false) continue;


    for (int j = i + 1; j < n; j++) {
      if (bit & (1 << j)) continue;
      if (cs[i].second != cs[j].second) continue;
      if (can_remove(j, bit) == false) continue;

      res = std::max(res, calc(bit | (1 << i) | (1 << j), cnt + 2, cs));
    }
  }

  return res;
}

void solve(int n) {
  std::vector< CI > cs; 

  dp = std::vector< int >(1 << n, -1);
  mask = std::vector< int >(n);
  for (int i = 0; i < n; i++) {
    geometry::circle<geometry::f80> cir;
    int c;
    std::cin >> cir.o >> cir.r >> c;

    cs.emplace_back(cir, c);
  }

  precalc(cs);
  std::cout << calc(0, 0, cs) << std::endl;
}

int main() {
  int n;

  while (std::cin >> n, n) {
    solve(n);
  }
}
#line 1 "test/aoj/icpc/1175.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1175

#line 2 "src/real-geometry/class/circle.hpp"

#line 2 "src/real-geometry/class/point.hpp"

#line 2 "src/real-geometry/class/vector.hpp"

#include <complex>
#include <iostream>

namespace geometry {

  template< typename R >
  class vec2d : public std::complex< R > {
    using complex = std::complex< R >;

   public:
    using complex::complex;

    vec2d(const complex &c): complex::complex(c) {}

    const R x() const { return this->real(); }
    const R y() const { return this->imag(); }

    friend vec2d operator*(const vec2d &v, const R &k) {
      return vec2d(v.x() * k, v.y() * k);
    }

    friend vec2d operator*(const R &k, const vec2d &v) {
      return vec2d(v.x() * k, v.y() * k);
    }

    friend std::istream &operator>>(std::istream &is, vec2d &v) {
      R x, y;
      is >> x >> y;
      v = vec2d(x, y);
      return is;
    }
 
  };

}
#line 4 "src/real-geometry/class/point.hpp"

#include <vector>

namespace geometry {

  template< typename R >
  using point = vec2d<R>;

  template< typename R >
  using points = std::vector< point< R > >;

}
#line 4 "src/real-geometry/class/circle.hpp"

#line 6 "src/real-geometry/class/circle.hpp"

// circle
namespace geometry {

  template< typename R >
  class circle {
   public:
    point<R> o;
    R r;

    circle() = default;
    circle(point<R> o, R r) : o(o), r(r) {}

    const point<R> center() const {
      return o;
    }

    const R radius() const {
      return r;
    }
  };


  template< typename R >
  using circles = std::vector< circle<R> >;

}
#line 2 "src/real-geometry/position/intersect-cc.hpp"

#line 2 "src/real-geometry/utility/equals/real-number.hpp"

#line 2 "src/real-geometry/utility/sign.hpp"

#line 2 "src/real-geometry/common/const/eps.hpp"

#line 2 "src/real-geometry/common/float-alias.hpp"

namespace geometry {

  using f80 = long double;
  using f64 = double;

}
#line 4 "src/real-geometry/common/const/eps.hpp"

namespace geometry {

  inline static f80 &eps() {
    static f80 EPS = 1e-10;
    return EPS;
  }

  void set_eps(f80 EPS) {
    eps() = EPS;
  }

}
#line 2 "src/real-geometry/numbers/sign.hpp"

#line 2 "src/real-geometry/common/int-alias.hpp"

namespace geometry {

  using i32 = int;
  using i64 = long long;

}
#line 4 "src/real-geometry/numbers/sign.hpp"

namespace geometry::number::sign {

  constexpr i32 PLUS  = +1;
  constexpr i32 ZERO  =  0;
  constexpr i32 MINUS = -1;

}
#line 5 "src/real-geometry/utility/sign.hpp"

namespace geometry {

  using namespace geometry::number::sign;

  template< typename R >
  inline int sign(R r) {
    if (r < -eps()) return MINUS;
    if (r > +eps()) return PLUS;
    return ZERO;
  }

}
#line 4 "src/real-geometry/utility/equals/real-number.hpp"

namespace geometry {

  template< typename R >
  bool equals(R a, R b) {
    return sign(a - b) == 0;
  }

}
#line 6 "src/real-geometry/position/intersect-cc.hpp"

#include <algorithm>
#line 9 "src/real-geometry/position/intersect-cc.hpp"

namespace geometry {

  constexpr int SEPERATE     = 4;
  constexpr int CIRCUMSCRIBE = 3;
  constexpr int INTERSECT    = 2;
  constexpr int INSCRIBE     = 1;
  constexpr int CONTAIN      = 0;

  template< typename R >
  int intersect_cc(circle<R> c1, circle<R> c2) {
    if (c1.r > c2.r) std::swap(c1, c2);
    R d = std::abs(c1.o - c2.o), r = c1.r + c2.r;

    if (sign(d - r) > 0) return SEPERATE;
    if (sign(d + c1.r - c2.r) < 0) return CONTAIN;
    if (equals(d, r)) return CIRCUMSCRIBE;
    if (equals(d + c1.r, c2.r)) return INSCRIBE;
    return INTERSECT;
  }

}
#line 5 "test/aoj/icpc/1175.test.cpp"

#line 9 "test/aoj/icpc/1175.test.cpp"

using CI = std::pair< geometry::circle<geometry::f80>, int >;
std::vector< int > dp;
std::vector< int > mask;

void precalc(const std::vector< CI > &cs) {
  int n = cs.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (geometry::intersect_cc(cs[i].first, cs[j].first) >= 3) continue;
      mask[i] |= (1 << j);
    }
  }
}

bool can_remove(int idx, int bit) {
  return (mask[idx] & bit) == mask[idx];
}

int calc(int bit, int cnt, const std::vector< CI > &cs) {
  if (dp[bit] != -1) return dp[bit];

  int n = cs.size();
  int &res = dp[bit];
  res = cnt;

  for (int i = 0; i < n; i++) {
    if (bit & (1 << i)) continue;
    if (can_remove(i, bit) == false) continue;


    for (int j = i + 1; j < n; j++) {
      if (bit & (1 << j)) continue;
      if (cs[i].second != cs[j].second) continue;
      if (can_remove(j, bit) == false) continue;

      res = std::max(res, calc(bit | (1 << i) | (1 << j), cnt + 2, cs));
    }
  }

  return res;
}

void solve(int n) {
  std::vector< CI > cs; 

  dp = std::vector< int >(1 << n, -1);
  mask = std::vector< int >(n);
  for (int i = 0; i < n; i++) {
    geometry::circle<geometry::f80> cir;
    int c;
    std::cin >> cir.o >> cir.r >> c;

    cs.emplace_back(cir, c);
  }

  precalc(cs);
  std::cout << calc(0, 0, cs) << std::endl;
}

int main() {
  int n;

  while (std::cin >> n, n) {
    solve(n);
  }
}
Back to top page