Buoys Gym - 101246J(三分)

The swimming area of Berhattan’s city beach is marked out with n buoys. The buoys form a straight line. When the buoys were being put into the water, nobody cared to observe the same distance between each pair of adjacent buoys.

Now the beach keeper wants the distance between any two adjacent buoys to be the same. He plans to shift some or all of the buoys without changing their respective order. To facilitate the task, he wants the total length of all shifts to be as small as possible.

Given coordinates of the buoys, you should find the minimum possible length of all shifts, as well as new coordinates of the buoys.

Input
The first line of input contains a single integer n (2 ≤ n ≤ 400), n — the number of buoys. The second line contains buoys’ integer coordinates x1, x2, …, xn ( - 10000 ≤ xi ≤ 10000). No two given buoys will share the same place. The coordinates are given in strictly increasing order.

Output
To the first line print a real number t — the minimum possible total length of required shifts. Output this value with at least 4 digits after the decimal point.

To the second line print n numbers — new coordinates of the buoys. The new coordinates should be printed in strictly increasing order with at least 7 digits after the decimal point. If there are several optimal ways to shift the buoys, you may output any of them.

Examples

input
4
-2 2 6 9
output
1.0000
-2.0000000000 1.6666666667 5.3333333333 9.0000000000

Note
All buoys are located on the Ox axis. You may move buoys only along the Ox axis.

大致题意:在x轴上有n个点,你可以任意移动它们,使得它们之间间隔的距离相等,问最少需要移动的总距离,并且让你输出移动后的n个点的坐标。

思路:很容易想到对每个点的间距x进行三分,然后考虑怎么求在间距x下的最少的移动距离,想要移动的距离最少,那么肯定有一个点是保持不动的,然后移动其它的点,所以我们枚举那个不动的点,剩下的每个点移动后的位置就很好求了。然后取最少的移动距离作为答案即可,记录一下枚举的那个不动点。

代码如下

#include <cstdio>  
#include <cstring>  
#include <iostream> 
#include <algorithm>
#include <map>   
#include <cmath>
using namespace std; 
#define ll long long int 

int n;
int wei[405];
int flag;//记录不动点的位置
double solve(double x)//求在间距为x的条件下的最少移动的距离
{
    double sum1=1e8;
    for(int i=1;i<=n;i++)
    {
        double sum=0;
        for(int j=1;j<i;j++)//不动点左边需要移动的总距离
        {
            double w=wei[i]-(i-j)*x;
            if(w>wei[j])
            sum+=w-wei[j];
            else if(w<wei[j])
            sum+=wei[j]-w;
        }

        for(int j=i+1;j<=n;j++)//加上不动点右边需要移动的总距离
        {
            double w=wei[i]+(j-i)*x;
            if(w>wei[j])
            sum+=w-wei[j];
            else if(w<wei[j])
            sum+=wei[j]-w;
        }
        if(sum1>sum)
        {
            sum1=sum;
            flag=i;
        }
    }
    return sum1;
}
int main() 
{  freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&wei[i]);
    sort(wei+1,wei+n+1);

    double l=0,r=1e4;
    double m1,m2;
    int t=100;//设置三分的次数,保证精度
    while(t--)
    {
        m1=(l+r)/2.0;
        m2=(r+m1)/2.0;
        if(solve(m1)>solve(m2))
        {
           l=m1;
        }
        else 
        {
            r=m2;
        }
    }

    double x=r;
    printf("%.4f\n",solve(r));

    double a[405];//保存移动后的每个点的坐标
    a[flag]=wei[flag];
    for(int j=1;j<flag;j++)
    {
    a[j]=wei[flag]-(flag-j)*x;
    }
    for(int j=flag+1;j<=n;j++)
    {
        a[j]=wei[flag]+(j-flag)*x;
    }

    for(int i=1;i<=n;i++)
    printf("%.10lf ",a[i]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值