枚举 + 三分 (游标)

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.

Example
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 轴上有一串浮标,移动一些浮标,让他们之间的距离相等,问最小移动的总距离之和是多少,并输出此时的浮标位置

思路 : 对于两个浮标间的距离 X ,当X过大或过小的时候都会使答案很大,因此中间存在一个最小值,这里三分就可以,然后既然要使距离最小,那我们就先确保一个浮标不动去移动其他的,这里枚举就可以。

代码示例:

#define ll long long
const int maxn = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

double pre[405];
double ans[405];
double sum = 1.0*inf, lenn;
int n, pos;

double fun(double len, int base){
    double x = pre[base];
    double s = 0;
    for(int i = base-1; i >= 1; i--){
        x -= len;
        s += fabs(x-pre[i]);
    }
    x = pre[base];
    for(int i = base+1; i <= n; i++){
        x += len;
        s += fabs(x-pre[i]);
    }
    if (s < sum) {
        sum = s;
        lenn = len; pos = base;
    } 
    return s;
}

int main() {
   freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    
    cin >> n;
    for(int i = 1; i <= n; i++){
        scanf("%lf", &pre[i]);
    }
    
    for(int i = 1; i <= n; i++){
        double l = 0, r = 200000;
        for(int j = 1; j <= 100; j++){
            double lm = l + (r-l)/3;
            double rm = r - (r-l)/3;
            double lf = fun(lm, i);
            double rf = fun(rm, i);
            if (lf > rf) l = lm;
            else r = rm; 
        }   
    }
    ans[pos] = pre[pos];
    for(int i = pos+1; i <= n; i++) ans[i] = ans[i-1] + lenn;
    for(int i = pos-1; i >= 1; i--) ans[i] = ans[i+1] - lenn; 
    printf("%.4f\n", sum);
    for(int i = 1; i <= n; i++) printf("%.10f%c", ans[i], i==n?'\n':' ');
    return 0;
}

 

转载于:https://www.cnblogs.com/ccut-ry/p/8459278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值