题意:给定一位空间里n个点的坐标,每个坐标有一个罪犯,现在要建一个警局,并且这个警局只有一辆车,车一次最多载m个人,问应建在哪是的抓回所有罪犯的路程和最小。

思路:

很明显建在罪犯的点上一定可以找到最优解。

那么直接枚举建在哪一个点。。

那么抓罪犯肯定从两边抓最优。所以预处理两个数组即可。。

code:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[], n, m;
ll sl[], sr[]; void solve(){
for (int i = ; i <= n; ++i){
scanf("%d", &a[i]);
if ((i-) % m == ) sl[i] = sl[max(i-m, )] + a[i];
}
for (int i = n; i >= ; --i)
if ((n-i) % m == ) sr[i] = sr[min(n+, i + m)] + a[i];
ll ans = 1LL<<;
int l, r, lr, rr;
int cnt = (n - ) / m + ;
rr = n - (cnt-) * m;
// cout << rr << endl;
ans = sr[rr] - (ll)cnt * a[];
// cout << ans << endl;
lr = + (cnt-) * m;
ans = min((ll)cnt * a[n] - sl[lr], ans);
// cout << ans << endl;
ll tmp, cnt1, cnt2;
for (int i = ; i <= n; ++i){
l = i - , r = i;
cnt1 = (l - ) / m + , cnt2 = (n - r) / m + ;
lr = + (cnt1 - ) * m, rr = n - (cnt2 - ) * m;
// if (i == 2){
// printf("lr = %d rr = %d\n", lr, rr);
// }
tmp = (ll)a[r] * cnt1 - sl[lr] + sr[rr] - (ll)a[r] * cnt2;
// printf("%d %lld\n", i, tmp);
ans = min(ans, tmp);
}
cout << ans * << endl;
}
int main(){
// freopen("a.in", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF){
solve();
}
}

codeforces 427E的更多相关文章

  1. Codeforces 427E Police Patrol

    找中间的数,然后从两头取. #include<stdio.h> ; int pos[MAX]; int main() { int n,m,tmp; int i; int pol; long ...

  2. 2017年暑假ACM集训日志

    20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Spark Streaming 事务处理彻底掌握

    本期内容: 1. Exactly once容错 2. 数据输出不重复 一. 事务场景 : 以银行转帐一次为例,A用户转账给B用户,如何保证事务的一致性,即A用户能够转出且只能转出一次,B用户能够收到且 ...

  2. 实现Base64加密解密

    using System; using System.Text;   namespace Common { /// <summary> /// 实现Base64加密解密 /// </ ...

  3. PCI Express(一)- Connector

    在FPGA4FUN上看到一篇介绍PCI-E的帖子,简单易懂,适合入门,特地搬过来 原文地址:http://www.fpga4fun.com/PCI-Express.html 前言: As PCI Ex ...

  4. Reactjs 入门基础(三)

    State 和 Props以下实例演示了如何在应用中组合使用 state 和 props .我们可以在父组件中设置 state, 并通过在子组件上使用 props 将其传递到子组件上.在 render ...

  5. 你还没成为Delphi QC的成员吗?(转红鱼儿)

    Delphi很早就建立了quality.embarcadero.com,简称为QC,质量控制中心,用来接收用户反馈的bug,新功能建议等,是开发者与delphi官方直接交流的平台.无论是否为正版用户, ...

  6. http的应用httpurlconnection--------1

    http请求后获得所需要的是字符串的时候 URL url=new URL(strurl); try { HttpURLConnection conn=(HttpURLConnection) url.o ...

  7. Linux与Windows API对比

    对象 操作 Linux API Windows API 线程 创建 pthread_create() CreateThread() 退出 pthread_exit() ThreadExit() 等待 ...

  8. C# 队列集合的使用

    using System; using System.Collections.Generic; using System.Text; using System.Collections; namespa ...

  9. Java 在某一个时间点定时执行任务(转载)

    java定时任务,每天定时执行任务.以下是这个例子的全部代码. public class TimerManager { //时间间隔 private static final long PERIOD_ ...

  10. OpenSSL命令---passwd

    NAME passwd - compute password hashes SYNOPSIS openssl passwd [-crypt] [-1] [-apr1] [-salt string] [ ...