找中间的数,然后从两头取。

#include<stdio.h>
const int MAX = ;
int pos[MAX];
int main()
{
int n,m,tmp;
int i;
int pol;
long long ans,dis;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&pos[i]);
pol=n/+;
tmp=m;
ans=;
for(i=;i<=pol;)
{
dis=;
while(i<=pol&&tmp--)
{
if(!dis)dis=pos[pol]-pos[i];
i++;
}
ans=ans+dis*;
tmp=m;
}
for(i=n;i>pol;)
{
dis=;
while(i>pol&&tmp--)
{
if(!dis)dis=pos[i]-pos[pol];
i--;
}
ans=ans+dis*;
tmp=m;
}
printf("%I64d\n",ans);
}
return ;
}

更加简洁的代码

1<<n 就是把1向左移N位
m>>=1 相当于m= m>>1,就是把m左移1位
#include <stdio.h>
using namespace std;
const int MAX_N = + ;
int n, m;
int arr[MAX_N];
int main()
{
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++)
scanf("%d", &arr[i]);
int mid = n >> ;
//printf("mid:%d\n",mid);
long long res = ;
for(int i = n - ; i > mid; i -= m)
res += arr[i] - arr[mid];
for(int i = ; i < mid; i += m)
res += arr[mid] - arr[i];
res = res << ;
printf("%I64d\n", res);
return ;
}

Codeforces 427E Police Patrol的更多相关文章

  1. codeforces 427E

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

  2. CodeForces - 796D Police Stations bfs

    思路:删除尽量多的边使得所有点都能在限制距离之内到达一个警局,删除边会形成多棵子树,最多只能k棵.其实就是以每个警局为根结点,把整棵树划分为以警局为根结点的k棵树,说明要删除的边的数量就是k-1条,即 ...

  3. CodeForces 427A Police Recruits (水题)

    题意:给定 n 个数,有正数和-1, -1表示罪犯,正数表示招了几个警察,一个警察只能看一个罪犯,并且要按顺序,问你有多少罪犯逃脱. 析:很简单么,从开始扫到最后,把是正数就加上,是-1判断剩余警察大 ...

  4. Codeforces Round #244 (Div. 2)

    今天是水题集啊.... A. Police Recruits time limit per test 1 second memory limit per test 256 megabytes inpu ...

  5. xtu summer individual 6 D - Checkposts

    Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  6. 2017年暑假ACM集训日志

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

  7. Codeforces Round #130 (Div. 2) C - Police Station 最短路+dp

    题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 seconds ...

  8. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  9. Codeforces Round #130 (Div. 2) C. Police Station

    题目链接:http://codeforces.com/contest/208/problem/C 思路:题目要求的是经过1~N的最短路上的某个点的路径数 /  最短路的条数的最大值.一开始我是用spf ...

随机推荐

  1. LeetCode Maximum Swap

    原题链接在这里:https://leetcode.com/problems/maximum-swap/description/ 题目: Given a non-negative integer, yo ...

  2. CODEVS1049 棋盘染色

    题目大意:01矩阵,1表示黑色,0表示白色,求将白色染成黑色最少的次数 使黑色成为一整个联通块. 题解: 搜索bfs 90... dfs判断连通 #include<iostream> #i ...

  3. fn project Message Queues 配置

      Message Queues A message queue is used to coordinate asynchronous function calls that run through ...

  4. Android 杂记

    Android Studio 报错:sdk location should not contain whitespace as this can cause problems with the ndk ...

  5. 连续创建多个Oracle触发器失败,单个创建才成功的解决方法

    当用到自增序列,需要用到触发器的时候,当我连续执行创建多个触发器时,总是报编译通过,但存在警告或错误. ---.应用信息 drop table app_info cascade constraints ...

  6. Oracle Database 12.2新特性详解

    在2015年旧金山的Oracle OpenWorld大会上,Oracle发布了Database 12.2的Beta版本,虽然Beta版本只对部分用户开放,但是大会上已经公布了12.2的很多重要的新特性 ...

  7. CAN总线位仲裁技术

    CAN总线位仲裁技术 只要总线空闲,任何单元都可以开始发送报文. 要对数据进行实时处理,就必须将数据快速传送,这就要求数据的物理传输通路有较高的速度.在几个站同时需要发送数据时,要求快速地进行总线分配 ...

  8. python3+ros+telnet+telnetlib

    利用python3的telnetlib模块 远程登录ros,输入帐号密码,然后执行命令,并导出结果到txt文本: 不过实际操作这种方式不行,因为telnet导出来的文本文件,带颜色编码,根本无法看哦. ...

  9. JCE无限制权限策略文件

    JCE无限制权限策略文件,里面是对应jdk6和jdk7的文件 官网下载地址是 JDK6:http://www.oracle.com/technetwork/java/javase/downloads/ ...

  10. WP8.1通过StreamSocket连接C++服务器

    注:当服务端和手机模拟器运行在一台机器时,会有奇怪错误.将服务端放在其它机器上更改客户端连接地址,运行正常.或者直接用本机modern调试也可以. 实例化一个对象 StreamSocket clien ...