Codeforces 427E Police Patrol
找中间的数,然后从两头取。
#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的更多相关文章
- codeforces 427E
题意:给定一位空间里n个点的坐标,每个坐标有一个罪犯,现在要建一个警局,并且这个警局只有一辆车,车一次最多载m个人,问应建在哪是的抓回所有罪犯的路程和最小. 思路: 很明显建在罪犯的点上一定可以找到最 ...
- CodeForces - 796D Police Stations bfs
思路:删除尽量多的边使得所有点都能在限制距离之内到达一个警局,删除边会形成多棵子树,最多只能k棵.其实就是以每个警局为根结点,把整棵树划分为以警局为根结点的k棵树,说明要删除的边的数量就是k-1条,即 ...
- CodeForces 427A Police Recruits (水题)
题意:给定 n 个数,有正数和-1, -1表示罪犯,正数表示招了几个警察,一个警察只能看一个罪犯,并且要按顺序,问你有多少罪犯逃脱. 析:很简单么,从开始扫到最后,把是正数就加上,是-1判断剩余警察大 ...
- Codeforces Round #244 (Div. 2)
今天是水题集啊.... A. Police Recruits time limit per test 1 second memory limit per test 256 megabytes inpu ...
- xtu summer individual 6 D - Checkposts
Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...
- 2017年暑假ACM集训日志
20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...
- 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 ...
- Codeforces Round #408 (Div. 2) D - Police Stations
地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...
- Codeforces Round #130 (Div. 2) C. Police Station
题目链接:http://codeforces.com/contest/208/problem/C 思路:题目要求的是经过1~N的最短路上的某个点的路径数 / 最短路的条数的最大值.一开始我是用spf ...
随机推荐
- matlab中求逆矩阵的高斯消元法实现的代码
function qiuni =INV_GET(a)N=length(a);M=eye(N);%得到上三角矩?for i=1:N max=a(i,i); A=i; for j=i+1:N if(abs ...
- Python函数-repr()
repr(object) 作用: repr() 函数将对象转化为供解释器读取的形式. object --对象.返回一个对象的 string 格式. 实例: >>>s = 'RUNOO ...
- 笔记:开源协议 Apache 2 和 GPL 兼容
笔记:开源协议 Apache 2 和 GPL 兼容 Apache 2 和 GPL v3 兼容. GPL 分了很多版本,LGPL 为最宽松的 GPL,而 AGPL 为最严格的 GPL 协议. Linux ...
- (转)App工程结构搭建:几种常见Android代码架构分析
关于Android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的角度,看到整齐的代码,优美的分层总是一种舒服的享受的. 从艺术的角度看,其实我们是在追求一种美. 本文先分析几个当 ...
- 在rebar发布的项目中添加监视工具
默认使用rebar创建的项目没法使用observer,可以如下操作 修改app.src {application, tcp_server, [ {description, ""}, ...
- Vue.js:监听属性
ylbtech-Vue.js:监听属性 1.返回顶部 1. Vue.js 监听属性 本章节,我们将为大家介绍 Vue.js 监听属性 watch,我们可以通过 watch 来响应数据的变化: 实例 & ...
- PL/SQL 训练01--基础介绍
--开始介绍变量之前,我们先看下怎么在PLSQL写程序,如下我们写了一个块 declare --声明部分,声明变量 v_name ) :='hello world'; begin --执行区域 dbm ...
- SDRAM 之时序收敛(学习了特权老师)
到现在我还是不太理解SDRAM的时序设置,但是可能蒙对了.(呵呵) 开发环境: quartus II 13.0 板子: DE2 EP2C35F672C6N 时序约束step 1:create cl ...
- js实现的点击div区域外隐藏div区域
首先看下JS的事件模型,JS事件模型为向上冒泡,如onclick事件在某一DOM元素被触发后,事件将跟随节点向上传播,直到有click事件绑定在某一父节点上,如果没有将直至文档的根. 阻止冒泡:1.对 ...
- 详解调试Apache的mod_rewrite模块
大家都知道Apache里面的Rewrite规则是一件很蛋疼的事情,有时候只是想做一个伪静态而已,不想去研究那些复杂的规则,可官方给的规则又常常出错,出了问题我们就要调试一下,看看提交的参数被映射到了哪 ...