Codeforces352-div2C Recycling Bottles

Problem - C - Codeforces

It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position .

荷物は一個しか持てないので,毎回拾って戻して,拾って戻してを繰り返す.なので,まずそれぞれから から行く道と帰る道を両方張る.それから が最初に拾った方が良いものがあれば拾う. が最初に拾った方が良いものがあれば拾う.とする
また,が動かず が全部拾う場合や, が動かず が拾う場合がある.最初に拾う点を選んでむしろ距離が増えてしまった場合は,その場に立ち止まるようにする.
また距離の初期値のINFがオーバーフローした.制約に気を付けたい.

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
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#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 INF 1LL<<60
#define pb push_back
#define mp make_pair
#define EPS 1e-8

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 main() {
  Point a, b, t;
  cin >> a.x >> a.y >> b.x >> b.y >> t.x >> t.y;

  int n;
  cin >> n;

  vector<Point> v(n);
  rep(i, n) cin >> v[i].x >> v[i].y;

  double sum = 0;
  rep(i, n) {
      sum += distancePP(t, v[i]) * 2;
  }

  double A = sum, B = sum;
  {
      double len = -INF;
      int id = -1;
      rep(i, n) {
          double p = distancePP(t, v[i]);
          double q = distancePP(a, v[i]);

          if(p - q > len) {
              len = p - q;
              id = i;
          }
      }

      double len2 = 0;
      int id2 = -1;
      rep(i, n) {
          if(i == id) continue;

          double p = distancePP(t, v[i]);
          double q = distancePP(b, v[i]);

          if(p - q > len2) {
              len2 = p - q;
              id2 = i;
          }
      }

      // cout <<  " A -> B" << endl;
      // cout << id << " " << id2 << endl;
      // cout << "sum:" << sum << endl;
      // cout << distancePP(t, v[id]) << " " << distancePP(a, v[id]) << " " << len << endl;
      // cout << distancePP(t, v[id2]) << " " << distancePP(b, v[id2]) << " " << len2 << endl;

      if(id != -1) A -= len;
      if(id2 != -1) A -= len2;
  }
  {
      double len = -INF;
      int id = -1;

      rep(i, n) {
          double p = distancePP(t, v[i]);
          double q = distancePP(b, v[i]);

          if(p - q > len) {
              len = p - q;
              id = i;
          }
      }

      double len2 = 0;
      int id2 = -1;
      rep(i, n) {
          if(i == id) continue;

          double p = distancePP(t, v[i]);
          double q = distancePP(a, v[i]);

          if(p - q > len2) {
              len2 = p - q;
              id2 = i;
          }
      }

      // cout <<  " B -> A" << endl;
      // cout << id << " " << id2 << endl;
      // cout << "sum:" << sum << endl;
      // cout << distancePP(t, v[id]) << " " << distancePP(b, v[id]) << " " << len << endl;
      // cout << distancePP(t, v[id2]) << " " << distancePP(a, v[id2]) << " " << len2 << endl;

      if(id != -1) B -= len;
      if(id2 != -1) B -= len2;
  }

  cout << fixed;
  cout.precision(20);
  cout << min(A, B) << endl;

  return 0;
}
May 12th, 2016