Codeforces Round #340 (Div. 2) 617C Watering Flowers(计算几何)
2016-01-26 11:01
411 查看
C. Watering Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A flowerbed has many flowers and two fountains.
You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0),
giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that
all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1,
or the distance to the second fountain doesn't exceed r2.
It's OK if some flowers are watered by both fountains.
You need to decrease the amount of water you need, that is set such r1 and r2 that
all the flowers are watered and the r12 + r22 is
minimum possible. Find this minimum value.
Input
The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) —
the number of flowers, the coordinates of the first and the second fountain.
Next follow n lines. The i-th
of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) —
the coordinates of the i-th flower.
It is guaranteed that all n + 2 points in the input are distinct.
Output
Print the minimum possible value r12 + r22.
Note, that in this problem optimal answer is always integer.
Sample test(s)
input
output
input
output
Note
The first sample is (r12 = 5, r22 = 1):
The
second sample is (r12 = 1, r22 = 32):
题目链接:点击打开链接
给出n个点坐标, 2个圆点坐标, 求最小的r1, r2平方和, 使得所有点包含在两个圆点组成的圆中.
求出各个点到两圆点坐标距离的平方, 按照距离由大到小排序, 枚举到第二个圆点距离最长, 与到第一个圆点距离和最小.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
ll n, x11, y11, x2, y2, tmp, ans = 1e18;
std::vector<pair<ll, ll> > v;
ll dis1(ll x, ll y)
{
return (x - x11) * (x - x11) + (y - y11) * (y - y11);
}
ll dis2(ll x, ll y)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
int main(int argc, char const *argv[])
{
cin >> n >> x11 >> y11 >> x2 >> y2;
for(int i = 0; i < n; ++i) {
ll x, y;
cin >> x >> y;
ll d1 = dis1(x, y), d2 = dis2(x, y);
v.push_back(make_pair(d1, d2));
}
sort(v.rbegin(), v.rend());
for(int i = 0; i < n; ++i) {
ans = min(ans, tmp + v[i].first);
tmp = max(tmp, v[i].second);
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A flowerbed has many flowers and two fountains.
You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0),
giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that
all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1,
or the distance to the second fountain doesn't exceed r2.
It's OK if some flowers are watered by both fountains.
You need to decrease the amount of water you need, that is set such r1 and r2 that
all the flowers are watered and the r12 + r22 is
minimum possible. Find this minimum value.
Input
The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) —
the number of flowers, the coordinates of the first and the second fountain.
Next follow n lines. The i-th
of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) —
the coordinates of the i-th flower.
It is guaranteed that all n + 2 points in the input are distinct.
Output
Print the minimum possible value r12 + r22.
Note, that in this problem optimal answer is always integer.
Sample test(s)
input
2 -1 0 5 3 0 2 5 2
output
6
input
4 0 0 5 0 9 4 8 3 -1 0 1 4
output
33
Note
The first sample is (r12 = 5, r22 = 1):
The
second sample is (r12 = 1, r22 = 32):
题目链接:点击打开链接
给出n个点坐标, 2个圆点坐标, 求最小的r1, r2平方和, 使得所有点包含在两个圆点组成的圆中.
求出各个点到两圆点坐标距离的平方, 按照距离由大到小排序, 枚举到第二个圆点距离最长, 与到第一个圆点距离和最小.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
ll n, x11, y11, x2, y2, tmp, ans = 1e18;
std::vector<pair<ll, ll> > v;
ll dis1(ll x, ll y)
{
return (x - x11) * (x - x11) + (y - y11) * (y - y11);
}
ll dis2(ll x, ll y)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
int main(int argc, char const *argv[])
{
cin >> n >> x11 >> y11 >> x2 >> y2;
for(int i = 0; i < n; ++i) {
ll x, y;
cin >> x >> y;
ll d1 = dis1(x, y), d2 = dis2(x, y);
v.push_back(make_pair(d1, d2));
}
sort(v.rbegin(), v.rend());
for(int i = 0; i < n; ++i) {
ans = min(ans, tmp + v[i].first);
tmp = max(tmp, v[i].second);
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
相关文章推荐
- php类的继承性及其相关
- java多线程之龟兔赛跑问题
- 浅谈网站失效的认证和会话管理
- Android 开发中使用 SQLite 数据库
- Java时间日期相关开发
- centos 增加扩展源EPEL
- MyEclipse2014安装插件的几种方式(适用于Eclipse或MyEclipse其他版本)
- 阿岳之_DNS主从配置篇(二)
- MSC.SIMUFACT.FORMING.V13.3金属成形工艺仿真软件+MSC.SIMUFACT.WELDING.V5.0仿真软件
- 谈谈网站测试中的AB测试方法
- Struts2.1 中 ONGL 表达式的应用总结
- maven打包:将webapp下的classes文件打包到单独的jar包中
- Java多继承和多实现 怎么回事?
- java中排序与查找
- UICollectionView must be initialized with a non-nil layout parameter
- 扩展Baby Step Giant Step (解(a^x)%n=b,其中c没有限制)
- IT人35岁之前的积累
- RAC11g 新增另一个网卡,DG同步
- 手动脱FSG壳实战
- 第 3 章 与浏览者交互,表单标签