SRM312 D2H PizzaDivision

TopCoder Statistics - Problem Statement

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

ピザにトッピングが載っている.トッピングを平等に分ける切り方は何通りあるか?


まず切り方が無限にあるのはトッピングが1つかつ原点の場合のみなので,それは場合分けしておく.それ以外の場合は原点にあるトッピングは無視して,そのトッピングと原点を結ぶ直線を作る.また,全ての二点間の中点と原点を結ぶ直線も作る.
あとはその直線がトッピングを平等に分ける直線となっていくか見ていく.点が最大で$50$と少ないので,ある点と今見ている直線との距離が等しい別の点を全探索し,$1$つでもあればその直線と点に対する線対称な点が存在するとして判定した.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define EPS 1e-9
#define equals(a,b) fabs((a) - (b)) < EPS
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct Point {
  double x, y;

  Point(double x=0, double y=0) : x(x), y(y) {}

  Point operator+(const Point &o) const { return Point(x+o.x, y+o.y); }

  Point operator-(const Point &o) const { return Point(x-o.x, y-o.y); }

  Point operator*(const double m) const { return Point(x*m, y*m); }

  Point operator/(const double d) const { return Point(x/d, y/d); }

  bool operator<(const Point &o) const { return x != o.x ? x < o.x : y < o.y; }

  bool operator==(const Point &o) const { return fabs(x-o.x) < EPS && fabs(y-o.y) < EPS; }
};

ostream& operator << (ostream& os, const Point& p) {
  os << "(" << p.x << ", " << p.y << ")";
  return os;
}

double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double atan(Point p) { return atan2(p.y, p.x); }
double norm(Point p) { return p.x * p.x + p.y * p.y; }
double abs(Point p) { return sqrt(norm(p)); }
double distancePP(Point p, Point o) { return sqrt(norm(o - p)); }

int ccw(Point a, Point b, Point c) {
  b = b-a;
  c = c-a;

  if(cross(b, c) > 0.0) return +1;  //conter clockwise
  if(cross(b, c) < 0.0) return -1;  //clockwise
  if(dot(b, c) < 0.0) return +2;    //a on Seg(b,c)
  if(norm(b) < norm(c)) return -2;   //b on Seg(a,c)
  return 0; //c on Seg(a,b)
}

struct Line {
  Point a, b;

  Line() : a(Point(0, 0)), b(Point(0, 0)) {}

  Line(Point a, Point b) : a(a), b(b) {}
};

ostream& operator << (ostream& os, const Line& l) {
  os << "(" << l.a.x << ", " << l.a.y << ")-(" << l.b.x << "," << l.b.y << ")";
  return os;
}

struct Seg {
  Point a,b;

  Seg() : a(Point(0, 0)), b(Point(0, 0)) {}

  Seg (Point a, Point b) : a(a),b(b) {}
};

ostream& operator << (ostream& os, const Seg& s) {
  os << "(" << s.a.x << ", " << s.a.y << ")-(" << s.b.x << "," << s.b.y << ")";
  return os;
}

bool isOrthogonal(Line l1, Line l2) { return equals(dot((l1.b - l1.a), (l2.b - l2.a)), 0.0); }

bool isParallel(Line l1, Line l2) { return equals(cross((l1.b - l1.a), (l2.b - l2.a)), 0.0); }

bool sameLine(Line l1, Line l2) { return abs(cross(l1.b - l1.a, l2.b - l2.a)) < EPS; }

bool isIntersectLL(Line l1, Line l2) { return !isParallel(l1, l2) || sameLine(l1, l2); }

bool isIntersectLS(Line l, Seg s) {
  return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < 0;
}

bool isIntersectSS(Seg s1, Seg s2) {
  return ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) <= 0
      && ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;
}

double distanceLP(Line l, Point p) {
  return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);
}

double distanceLS(Line l, Seg s) {
  if (isIntersectLS(l, s)) return 0.0;
  return min(distanceLP(l, s.a), distanceLP(l, s.b));
}

double distanceSP(Seg s, Point p) {
  if (dot(s.b - s.a, p - s.a) < 0.0) return abs(p - s.a);
  if (dot(s.a - s.b, p - s.b) < 0.0) return abs(p - s.b);
  return distanceLP(Line(s.a, s.b) , p);
}

double distanceSS(Seg s1, Seg s2) {
  if (isIntersectSS(s1, s2)) return 0.0;
  return min( min(distanceSP(s1, s2.a), distanceSP(s1, s2.b)), min(distanceSP(s2, s1.a), distanceSP(s2, s1.b)) );
}

// if isIntersectLL(l1, l2)
Point crossPointLL(Line l1, Line l2) {
  Point v = l1.b - l1.a;
  Point v2 = l2.b - l2.a;
  return l1.a + v * cross(v2, l2.a - l1.a) / cross(v2, v);
}

// if isIntersectLS(l, s)
Point crossPointLS(Line l, Seg s) { return crossPointLL(l, Line(s.a, s.b)); }

// if isIntersectSS(s1, s2)
Point crossPointSS(Seg s1, Seg s2) { return crossPointLL(Line(s1.a, s1.b), Line(s2.a, s2.b)); }

Point project(Line l, Point p) {
  Point base = l.b - l.a;
  double t = dot(base, p-l.a) / dot(base, base);
  return l.a + base * t;
}

Point reflect(Line l, Point p) {
  return p + (project(l, p) - p) * 2.0;
}

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos) {
      res.push_back(string(str, current, found - current));
      current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

vector<Line> uniqueLine(vector<Line> lines) {
  vector<Line> ret;
  rep(i, lines.size()) {
      bool flag = true;
      REP(j, i+1, lines.size()) {
          if(sameLine(lines[i], lines[j])) {
              flag = false;
          }
      }

      if(flag) ret.push_back(lines[i]);
  }

  return ret;
}

class PizzaDivision {

    public:

    int howMany(vector <string> toppings) {
      int n = toppings.size();

      vector<Point> v(n);
      rep(i, n) {
          vector<string> ret = split(toppings[i], ' ');
          double x, y;
          stringstream ss1(ret[0]);
          stringstream ss2(ret[1]);

          ss1 >> x;
          ss2 >> y;

          v[i].x = x;
          v[i].y = y;
      }

      if(n == 1 && equals(v[0].x, 0.0) && equals(v[0].y, 0.0)) return -1;

      vector<Line> lines;
      rep(i, n) {
          if(equals(v[i].x, 0) && equals(v[i].y, 0)) continue;
          Line l;
          l.a = v[i];
          l.b.x = 0.0;
          l.b.y = 0.0;
      
          lines.push_back(l);
      }

      rep(i, n) {
          REP(j, i + 1, n) {
              Line l;
              double x = (v[j].x + v[i].x) / 2;
              double y = (v[j].y + v[i].y) / 2;

              l.a.x = x;
              l.a.y = y;
              l.b.x = 0.0;
              l.b.y = 0.0;

              if(equals(x, 0.0) && equals(y, 0.0)) {
                  Point vec = v[i] - l.a;
                  x = vec.x * 0 - vec.y * 1;
                  y = vec.x * 1 + vec.y * 0;

                  l.b.x += x;
                  l.b.y += y;
              }

              lines.push_back(l);
          }
      }

      lines = uniqueLine(lines);

      int cnt = 0;
      rep(i, lines.size()) {
          bool flag = true;
          rep(j, n) {
              if(equals(distanceLP(lines[i], v[j]), 0.0)) continue;

              bool ch = false;
              rep(k, n) {
                  if(k == j) continue;

                  Line l(v[j], v[k]);

                  if(isOrthogonal(lines[i], l)) {
                      Point p = crossPointLL(lines[i], l);
                      double dist1 = distancePP(v[j], p);
                      double dist2 = distancePP(v[k], p);
                      if(equals(dist1, dist2)) {
                          ch = true;
                      }
                  }
              }

              if(ch) continue;
              flag = false;
          }

          if(flag) cnt++;
      }

      return cnt;
    }
};
Sep 30th, 2016