Codeforces 671 A——Recycling Bottles——————【思维题】
2 seconds
256 megabytes
standard input
standard output
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 (xi, yi). Both Adil and Bera can carry only one bottle at once each.
For both Adil and Bera the process looks as follows:
- Choose to stop or to continue to collect bottles.
- If the choice was to continue then choose some bottle and walk towards it.
- Pick this bottle and walk to the recycling bin.
- Go to step 1.
Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.
They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.
First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.
The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.
Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle.
It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.
Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .
3 1 1 2 0 0
3
1 1
2 1
2 3
11.084259940083
5 0 4 2 2 0
5
5 2
3 0
5 5
3 5
3 3
33.121375178000
Consider the first sample.
Adil will use the following path: .
Bera will use the following path: .
Adil's path will be units long, while Bera's path will be
units long.
题目大意: 给你ax,ay, bx,by, tx,ty。a、b两个人的坐标和垃圾桶的坐标。下面是n,然后n个瓶子的坐标xi,yi。两个人都只能拿到一个瓶子然后送回垃圾桶,然后再去捡其他瓶子。问你当所有瓶子都放入垃圾桶时,两个人一共走的最短距离是多少。
解题思路:假设a,b两人和垃圾桶在同一初始位置。那么所有瓶子放入垃圾桶时的距离为2*sigma(disti),dist表示垃圾桶到其他瓶子的距离,我们把这个值设为sum。现在考虑a如果开始捡第一瓶子i,那么所要走的距离为disa[i]-dist[i]+sum,考虑b如果开始捡第一个瓶子j,那么要走的距离为disb[i]-dist[i]+sum。现在我们维护两个数组a[i]表示a第一次捡i这个瓶子时要走的距离,b[i]表示b第一次要捡i瓶子时要走的距离。我们维护b最小的两个值,同时记录id。然后枚举a数组,同时,如果所维护的两个id中有一个是所枚举的a数组下标,那么b就取另一个,否则取最小的值更新结果。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int mod = 1e9+7;
const int maxn = 1e5+200;
const LL INF = 0x3f3f3f3f3f3f3f3f;
struct Coor{
double x, y;
}coors[maxn];
double Distan(Coor a, Coor b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double disa[maxn], disb[maxn];
int main(){
int n;
Coor a, b, t;
while(scanf("%lf%lf %lf%lf %lf%lf",&a.x,&a.y,&b.x,&b.y,&t.x,&t.y)!=EOF){
scanf("%d",&n);
double dt;
double sum = 0;
double opt1 = double(INF), opt2 = double(INF);
int opt1id = 1, opt2id = 1;
for(int i = 1; i <= n; i++){
scanf("%lf%lf",&coors[i].x,&coors[i].y);
dt = Distan(coors[i], t);
sum += 2.0*dt;
disa[i] = Distan(coors[i], a) - dt;
disb[i] = Distan(coors[i], b) - dt;
if(disb[i] < opt1){
swap(opt1, opt2);
swap(opt1id,opt2id);
opt1 = disb[i];
opt1id = i;
}else if(disb[i] < opt2){
opt2 = disb[i];
opt2id = i;
}
}
double ans = double(INF);
for(int i = 1; i <= n; i++){
ans = min(ans, sum + disa[i]);
}
for(int i = 1; i <= n; i++){
ans = min(ans, sum + disb[i]);
}
if(n == 1){
printf("%.8lf",ans); continue;
}
for(int i = 1; i <= n; i++){
if(i == opt1id){
ans = min(ans, sum + disa[i]+disb[opt2id]);
}else{
ans = min(ans, sum + disa[i] + disb[opt1id]);
}
}
printf("%.7lf",ans);
}
return 0;
}
Codeforces 671 A——Recycling Bottles——————【思维题】的更多相关文章
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- codeforces 672C C. Recycling Bottles(计算几何)
题目链接: C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
- 【18.69%】【codeforces 672C】Recycling Bottles
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
- Codeforces 1365G - Secure Password(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 首先考虑一个询问 \(20\) 次的方案,考虑每一位,一遍询问求出下标的这一位上为 \(0\) 的位置上值的 bitwise or,再一遍 ...
- Codeforces 1129E - Legendary Tree(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 考虑以 \(1\) 为根,记 \(siz_i\) 为 \(i\) 子树的大小,那么可以通过询问 \(S=\{2,3,\cdots,n\}, ...
- CodeForces - 427A (警察和罪犯 思维题)
Police Recruits Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- codeforces 848B Rooter's Song 思维题
http://codeforces.com/problemset/problem/848/B 给定一个二维坐标系,点从横轴或纵轴垂直于发射的坐标轴射入(0,0)-(w,h)的矩形空间.给出点发射的坐标 ...
随机推荐
- EDM模板制作规范
为了保证最大的兼容性,在制作HTML的email页面时,请严格按照规范来书写: 1.页面宽度推荐500px,最大不要超过750px: 2.制作HTML的email页面时,不使用css+div来布局,最 ...
- 构建基于asp.net core 的docker应用并发布
发布Docker镜像的方法有很多种,asp.net core的发布需要在windows系统中 开门见山,首先保证已经在Centos上安装好了Docker.创建一个asp.net core的webapi ...
- LAYABOX 开发遇到的问题记录
1. 如若在MAC下用LAYA开发H5游戏, 调试的时候会发现像素点过小(mac 5k屏),直接用下面按比例填充就好了 //保持原始高宽比的情况下,将舞台铺满屏幕,超出比例的部分会有黑边 ...
- D. Magic Box(几何)
One day Vasya was going home when he saw a box lying on the road. The box can be represented as a re ...
- [51nod] 1090 3个数和为0 暴力+二分
给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3个数的组合.如果没有这样的组合,输出No Solution.如果有多个,按照3个数中最小的数从小到 ...
- 原生态js展开收缩
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- input 实现onchange效果
$(".selected input").on('input',function(e){ cc.search(); });
- CodeChefSeries Sum (伯努利数+生成函数+FFT)
题面 传送门 给定\(a_1,..,a_n\),定义\(f(x,k)=\sum_{i=1}^n(x+a_i)^k,g(t,k)=\sum_{x=0}^tf(x,k)\),给定\(T,K\),请你对\( ...
- [USACO17DEC]Standing Out from the Herd(广义后缀自动机)
题意 定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数.如 "amy" 与 “tommy” 两个串,只属于 "amy" 的本质不同的子串为 ...
- [Objective-C语言教程]常量(7)
常量指的是程序在执行期间不会改变的固定值.这些固定值也称为文字.常量可以是任何基本数据类型,如整数常量,浮点常量,字符常量或字符串文字.还有枚举常量.常量被视为常规变量,只不过它们的值在定义后无法修改 ...