Codeforces Round #244 (Div. 2) E. Police Patrol(数学)

来源:互联网 发布:手机桌面便签软件 编辑:程序博客网 时间:2024/05/22 04:48

题目地址:http://codeforces.com/problemset/problem/427/E

思路:显然,位置选在所有数的中位数时距离最短。则从两头取,累计和即可。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int maxn=1e6+50;LL ans;int n,m;int a[maxn];int main(){    scanf("%d%d",&n,&m);    for(int i=1; i<=n; i++)        scanf("%d",&a[i]);    int p=n/2+1;    for(int i=1; i<=p; i+=m)        ans+=abs(a[i]-a[p]);    for(int i=n; i>p; i-=m)        ans+=abs(a[i]-a[p]);    printf("%I64d\n",ans*2);    return 0;}



0 0