CTU Open Contest 2019 F Beer Marathon(三分答案)

Beer Marathon
marathon.c, marathon.cpp, Marathon.java, marathon.py
In the booths version of the popular annual Beer Marathon, many beer booths (also called beer stalls or beer stands) are installed along the track. A prescribed number of visits to different beer booths is a part of the race for all contestants. The distances between any two consecutive beer booths should be exactly the same and equal to the particular value specified in the race rules.
The company responsible for beer booths installation did the sloppy work (despite being excessively paid), left the beer booths on more or less random positions along the track and departed from the town. Fortunately, thanks to advanced AI technology, the company was able to report the exact positions of the beer booths along the track, measured in meters, with respect to the particular anchor point on the track. The marathon committee is going to send out the volunteers to move the beer booths to new positions, consistent with the race rules.
They want to minimize the total number of meters by which all beer booths will be moved. The start line and the finishing line of the race will be chosen later, according to the resulting positions of the beer booths.
Input Specification
The first line of input contains two integers N and K (1 ≤ N,K ≤ 106), where N is the number of beer booths and K is the prescribed distance between the consecutive beer booths. The second line of input contains N pairwise different integers x1,... ,xN (−106 ≤ xi ≤ 106), the original positions, in meters, of the beer booths relative to the anchor point. The positive values refer to the positions past the anchor point and the negative values refer to the positions before the anchor point.
Output Specification
Output a single integer — the minimal total number of meters by which the beer booths should be moved to satisfy the race rules.
Sample Input 1
3 1

2 5 7


Output for Sample Input 1
3


Sample Input 2
10 4

140 26 69 55 39 64 2 89 78 421


Output for Sample Input

2
511

题意:

给出n个啤酒摊的坐标,规定每个啤酒摊间距为k,现移动啤酒摊使每个啤酒摊间之间的距离为k,问最短移动总长度

思路:

扫描二维码关注公众号,回复: 9686740 查看本文章

三分第一个啤酒摊的坐标,边界值最小1e12(不知道为啥.....),为了保证时间复杂度,规定循环100次。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int N = 1e6 +10;

int x[N], n, k;

ll solve(ll h)
{
    ll ans = 0;
    ll id = h;
    for(int i = 1; i <= n; ++i)
    {
        ans += abs(x[i] - id);
        id += k;
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d", &n, &k))
    {
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &x[i]);
        }
        sort(x + 1, x + n + 1);
        ll l = -1000000000000, r = 1000000000000;
        ll res = inf;
        for(int i = 1; i <= 100; ++i)
        {
            ll m1 = l + (r - l) / 3;
            ll m2 = r - (r - l) / 3;
            ll tmp1 = solve(m1);
            ll tmp2 = solve(m2);
            if(tmp1 > tmp2)
            {
                l = m1;
            }
            else
            {
                r = m2;
            }
            res = min(res, min(tmp1, tmp2));
        }
        cout<<res<<'\n';
    }
    return 0;
}

/*
3 1
2 5 7
10 4
140 26 69 55 39 64 2 89 78 421
*/
发布了188 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/104452741