Codeforces #427 E题Police Patrol(三分查找)

这题是三分查找模板题,虽然还没弄明白三分查找的原理,还是先放在这里吧。。

E. Police Patrol
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Imagine that your city is an infinite 2D plane with Cartesian coordinate system. The only crime-affected road of your city is the x-axis. Currently, there are n criminals along the road. No police station has been built on this road yet, so the mayor wants to build one.

As you are going to be in charge of this new police station, the mayor has asked you to choose a suitable position (some integer point) for building it. You should choose the best position for the police station, so that you could minimize the total time of your criminal catching mission. Your mission of catching the criminals will operate only from this station.

The new station will have only one patrol car. You will go to the criminals by this car, carry them on the car, bring them back to the police station and put them in prison. The patrol car can carry at most m criminals at a time. Note that, the criminals don't know about your mission. So, they will stay where they are instead of running away.

Your task is to find the position for the police station, so that total distance you need to cover to catch all the criminals will be minimum possible. Note that, you also can built the police station on the positions where one or more criminals already exist. In such a case all these criminals are arrested instantly.

Input

The first line of the input will have two integers n (1 ≤ n ≤ 106) and m (1 ≤ m ≤ 106) separated by spaces. The next line will contain nintegers separated by spaces. The ith integer is the position of the ith criminal on the x-axis. Absolute value of positions will not exceed 109. If a criminal has position x, he/she is located in the point (x, 0) of the plane.

The positions of the criminals will be given in non-decreasing order. Note, that there can be more than one criminal standing at some point of the plane.

Note: since the size of the input/output could be very large, don't use slow input/output techniques in your language. For example, do not use input/output streams (cin, cout) in C++.

Output

Print a single integer, that means the minimum possible distance you need to cover to catch all the criminals.

Sample test(s)
input
3 6
1 2 3
output
4
input
5 5
-7 -6 -3 -1 1
output
16
input
1 369
0
output
0
input
11 2
-375 -108 1336 1453 1598 1892 2804 3732 4291 4588 4822
output
18716

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include<algorithm>

using namespace std;
__int64 a[2000000];
__int64 Abs(__int64 x)
{
    return x>0?x:-x;
}
__int64 Cal(int x,int c,int n)
{
    __int64 ans = 0;
    int i;
    for(i = 1; i < x; i += c)
    {
        ans += Abs(a[i]-a[x]);
    }
    for(i = n; i > x; i -= c)
    {
        ans += Abs(a[i]-a[x]);
    }
    return ans*2;
}
__int64 find_min_num(__int64 *arr, int begin, int end,int c,int n)
{
    if(begin == end)
        return 0;
    int mid, midmid;
    __int64  max;
    __int64 t1,t2;
    while (begin < end)
    {
        if (begin + 1 == end)
        {
            t1 = Cal(begin,c,n);
            t2 = Cal(end,c,n);
            max = t1 < t2 ? t1 : t2;
            return max;
        }
        mid = (begin + end) / 2;
        if (mid + 1 == end)
        {
            midmid = (mid + end) / 2 + 1;
        }
        else
        {
            midmid = (mid + end) / 2;
        }
        if (midmid == end)
        {
            t1 = Cal(begin,c,n);
            t2 = Cal(mid,c,n);
            max = t1 < t2 ? t1 : t2;
            t1 = Cal(end,c,n);
            max = max < t1 ? max : t1;
            return max;
        }
        t1 = Cal(mid,c,n);
        t2 = Cal(midmid,c,n);

        if (t1 <= t2)
            end = midmid;
        else
            begin = mid;
    }

    return Cal(mid,c,n);
}
int main()
{
    int n, m, i;
    scanf("%d%d",&n,&m);
    for(i=1; i<=n; i++)
        scanf("%I64d",&a[i]);
    printf("%I64d\n",find_min_num(a,1,n,m,n));
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值