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 ...
随机推荐
- Spring Aware接口---BeanNameAware BeanFactoryAware ApplicationContextAware
前言 对于应用程序来说,应该尽量减少对spring api的耦合程度,然后有时候为了运用spring提供的一些功能,有必要让bean了解spring容器对其管理的细节信息,如让bean知道在容器中是以 ...
- Vue中render: h => h(App)的含义
// ES5 (function (h) { return h(App); }); // ES6 h => h(App); 官方文档 render: function (createElemen ...
- IE兼容模式与非兼容模式下jq的写法
1. $("#LabelRepeatType").removeAttr("disabled"); $("#LabelF ...
- grep 命令使用指南
grep 命令 grep参数: -E:等同于egrep -o:只匹配你想要的内容,下面是示例: [root@localhost ~]# cat /data/game/config/server_con ...
- sql中case when的简单使用
这是一个很多博客都引用的博客,作者未知,但是我第一次看到的就是这个,所以置顶这个吧, 这里有两个我刚才使用的列子: --查询同一机构的签约数和解约数: select t.sgn_acct_issr_i ...
- java代码求分数等级的输出~~~
总结:无论是switch-case-break语句 都不要忘了跳出当前循环,即break; 还有这个输入的分数我如何控制在100以内???? package com.c2; //实现分数等级的输出. ...
- oracle 分析函数加order by的影响
create table test (id number(2), name varchar2(10), salary number(6,2));insert into test values (1,' ...
- java成神之——集合框架之ArrayList,Lists,Sets
集合 集合种类 ArrayList 声明 增删改查元素 遍历几种方式 空集合 子集合 不可变集合 LinkedList Lists 排序 类型转换 取交集 移动元素 删除交集元素 Sets 集合特点 ...
- 引用WebService出现错误
在引用WebService作为服务引用的时候,由于VS生成时生成了两个入口点,导致出现 引发的异常:“System.Windows.Markup.XamlParseException”(位于 Pres ...
- redis学习六 集群的原理(转载)
转载自 http://shift-alt-ctrl.iteye.com/blog/2285470 一.Redis Cluster主要特性和设计 集群目标 1)高性能和线性扩展,最大可以支撑到1 ...