传送门:http://codeforces.com/contest/672/problem/C
题意:有两个人和一个垃圾桶,n个瓶子, 现在要人去把每个瓶子捡起来然后扔到垃圾箱中,每人最多带一个瓶子,求最短总距离
思路:
我先用坐标平移使得垃圾桶作为零点建立坐标系
假设人都在垃圾箱上,那么总距离为2 * 每个瓶子到垃圾桶的距离
现在人不在上面,所以只要求出瓶子到垃圾桶的距离 - 人到瓶子的距离的最大值
然后再用总和减去上面一行所求即可
注意:肯定要有一个人先捡瓶子再回垃圾箱的,如果另一个人的最大值小于0,那么久不需要他捡了, 还有注意只有1个瓶子的情况。
PS:这题不仅细节多,而且考验码力,还是要多多练习
代码:
#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e5+10;
double dis(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2));
}
struct node
{
double x,y;
int id;
}a[N], b[N];
int vis[N];
double ax, ay, bx, by, tx, ty, sum;
bool cmp1(node a, node b){
return dis(a.x, a.y, 0, 0) - dis(a.x, a.y, ax, ay) > dis(b.x, b.y, 0, 0) - dis(b.x, b.y, ax, ay);
}
bool cmp2(node a, node b){
return dis(a.x, a.y, 0, 0) - dis(a.x, a.y, bx, by) > dis(b.x, b.y, 0, 0) - dis(b.x, b.y, bx, by);
}
int main(){
cin>>ax>> ay>> bx>> by>> tx>> ty;
ax -= tx;//把垃圾桶作为坐标原点,坐标变换
ay -= ty;
bx -= tx;
by -= ty;
int n;
cin>>n;
for(int i=1; i<=n; i++){
double x, y;
cin>>x>>y;
b[i].x=a[i].x=x-tx;
b[i].y=a[i].y=y-ty;
a[i].id=b[i].id=i;
sum += 2*(dis(a[i].x, a[i].y, 0, 0));
}
double mx=0, a1, a2, b1, b2;
sort(a+1, a+n+1, cmp1); vis[a[1].id]=1;
a1=dis(a[1].x, a[1].y, 0, 0) - dis(a[1].x, a[1].y, ax, ay);
a2=dis(a[2].x, a[2].y, 0, 0) - dis(a[2].x, a[2].y, ax, ay);
sort(b+1, b+n+1, cmp2);
b1=dis(b[1].x, b[1].y, 0, 0) - dis(b[1].x, b[1].y, bx, by);
b2=dis(b[2].x, b[2].y, 0, 0) - dis(b[2].x, b[2].y, bx, by);
if(n==1){
mx=max(a1, b1);
}
else{
if(vis[b[1].id])
mx = max(max(a1, b2)+max(min(a1, b2), 0.0), max(a2, b1)+max(min(a2, b1), 0.0));//注意要和0比较
else
mx = max(a1, b1)+max(min(a1, b1), 0.0);
}
printf("%.12lf\n", sum-mx);
return 0;
}
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.

本文介绍了一种解决回收日问题的算法,通过坐标平移建立坐标系并计算两人的最短总行走距离来收集所有瓶子并投入回收箱。考虑了各种特殊情况,并提供了详细的代码实现。

4358

被折叠的 条评论
为什么被折叠?



