Codeforces 672C Recycling Bottles【极限思维+贪心枚举】

C. Recycling Bottles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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:

  1. Choose to stop or to continue to collect bottles.
  2. If the choice was to continue then choose some bottle and walk towards it.
  3. Pick this bottle and walk to the recycling bin.
  4. 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.

Input

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.

Output

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 .

Examples
Input
3 1 1 2 0 0
3
1 1
2 1
2 3
Output
11.084259940083
Input
5 0 4 2 2 0
5
5 2
3 0
5 5
3 5
3 3
Output
33.121375178000
Note

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)处,现在有N个垃圾,需要处理,垃圾箱位于(tx,ty)处,这两个人每次只能拿一个垃圾,并且拿到垃圾之后必须放到垃圾箱中,对于两个人来讲,每个人的行动都是相对独立的。问两个人的路径和最小是多少,就能够将所有垃圾都扔到垃圾箱中。


思路:


1、这种思维题还是靠某些典型题的体型所影响。比如51nod 1487 占领资源(topcoder上的题)这道题:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1487

利用到这种极限思维的题还是蛮经典的。可以规划到一类去深刻记忆。

因为做出来这个题的时候感觉这种极限思维还是蛮有趣的,记忆比较深刻。


2、对于A.B两个人来讲,如果一开始都在垃圾箱处出发,那么ans=2*Σdis(xi,yi)---->(tx,ty);

对于两个人A.B,每个人只要都选了一个垃圾去捡,并且都回到了垃圾箱处,那么接下来的路程就相当于一个人来回走。

那么对于暴力思维来讲,那就是直接O(n^2)枚举两个垃圾,过程维护最小值。显然这么做是会超时的。

那么接下里从贪心的角度出发,对于ans=2*Σdis(xi,yi)->(tx,ty)-A选择的第一个垃圾的点到垃圾箱的距离+A选择的第一个垃圾的点到A初始位子的距离-B选择的第一个垃圾的点到垃圾箱的距离+B选择的第一个垃圾的点到A初始位子的距离;

我们肯定是希望后边这一大长串描述中,A选择的第一个垃圾的点到A初始位子的距离以及B选择的第一个垃圾的点到B初始位子的距离的和尽可能的小,然而对于不同的选择方式,最终答案肯定是不同的。

那么我们不妨设定两个数组A【】,B【】,一个按照点与A的距离从小到大排序,一个按照与B的距离从小到大排序。

接下来我们可以暴力枚举两项,分别作为A选择的点以及B选择的点,当然不能忘记,有可能一个人一直不动是正解,这种情况也要枚举。


3、通过思考和简单枚举不难发现对于枚举的量,其实只要足够大,就一定不会影响结果。

所以在第一次提交的时候,我A,B数组都枚举了1000的量,结果是Ac的。(再之后交了一发100的枚举量,也是Ac的);


4、这种极限思维还是蛮实用的。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
struct node
{
    ll x,y;
    double dis0;
    double disa;
    double disb;
    int pos;
}a[100060],b[100060];
double dis(ll a,ll b,ll c,ll d)
{
    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
int cmp(node a,node b)
{
    if(a.disa!=b.disa)
    return a.disa<b.disa;
    else return a.disb<b.disb;
}
int cmp2(node a,node b)
{
    if(a.disb!=b.disb)
    return a.disb<b.disb;
    else return a.disa<b.disa;
}
int main()
{
    ll ax,ay,bx,by,tx,ty;
    while(~scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&ax,&ay,&bx,&by,&tx,&ty))
    {
        ll n;
        scanf("%I64d",&n);
        double minna=-1,minna0;
        double minnb=-1,minnb0;
        double sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
            a[i].pos=i;
            b[i].pos=i;
            a[i].dis0=dis(a[i].x,a[i].y,tx,ty);
            a[i].disa=dis(a[i].x,a[i].y,ax,ay);
            a[i].disb=dis(a[i].x,a[i].y,bx,by);
            sum+=2*a[i].dis0;
            b[i].dis0=a[i].dis0,b[i].disa=a[i].disa;b[i].disb=a[i].disb;
        }
        double ans=2000000000000000000;
        sort(a,a+n,cmp);
        sort(b,b+n,cmp2);
        for(int i=0;i<n&&i<100;i++)
        {
            for(int j=0;j<n&&j<100;j++)
            {
                if(a[i].pos==b[j].pos)
                {
                    ans=min(ans,min(sum-a[i].dis0+a[i].disa,sum-b[j].dis0+b[j].disb));
                    continue;
                }
                ans=min(ans,sum-a[i].dis0-b[j].dis0+a[i].disa+b[j].disb);
            }
        }
        printf("%.12lf\n",ans);
    }
}






带开环升压转换器和逆变器的太阳能光伏系统 太阳能光伏系统驱动开环升压转换器和SPWM逆变器提供波形稳定、设计简单的交流电的模型 Simulink模型展示了一个完整的基于太阳能光伏的直流到交流电力转换系统,该系统由简单、透明、易于理解的模块构建而成。该系统从配置为提供真实直流输出电压的光伏阵列开始,然后由开环DC-DC升压转换器进行处理。升压转换器将光伏电压提高到适合为单相全桥逆变器供电的稳定直流链路电平。 逆变器使用正弦PWM(SPWM)开关来产生干净的交流输出波形,使该模型成为研究直流-交流转换基本操作的理想选择。该设计避免了闭环和MPPT的复杂性,使用户能够专注于光伏接口、升压转换和逆变器开关的核心概念。 此模型包含的主要功能: •太阳能光伏阵列在标准条件下产生~200V电压 •具有固定占空比操作的开环升压转换器 •直流链路电容器,用于平滑和稳定转换器输出 •单相全桥SPWM逆变器 •交流负载,用于观察实际输出行为 •显示光伏电压、升压输出、直流链路电压、逆变器交流波形和负载电流的组织良好的范围 •完全可编辑的结构,适合分析、实验和扩展 该模型旨在为太阳能直流-交流转换提供一个干净高效的仿真框架。布局简单明了,允许用户快速了解信号流,检查各个阶段,并根据需要修改参数。 系统架构有意保持模块化,因此可以轻松扩展,例如通过添加MPPT、动态负载行为、闭环升压控制或并网逆变器概念。该模型为进一步开发或整合到更大的可再生能源模拟中奠定了坚实的基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值