codeforces 427E
题意:给定一位空间里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的更多相关文章
- Codeforces 427E Police Patrol
找中间的数,然后从两头取. #include<stdio.h> ; int pos[MAX]; int main() { int n,m,tmp; int i; int pol; long ...
- 2017年暑假ACM集训日志
20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- zedboard如何从PL端控制DDR读写(五)
有了前面的一堆铺垫.现在终于开始正式准备读写DDR了,开发环境:VIVADO2014.2 + SDK. 一.首先要想在PL端通过AXI去控制DDR,我们必须要有一个AXI master,由于是测试,就 ...
- 移动端自动化环境搭建-node.js的安装
安装node.js A.安装依赖 Appium是使用nodejs实现的,所以node是解释器,首先需要确认安装好 B.安装过程
- A Beginner's Guide To Understanding Convolutional Neural Networks(转)
A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural ...
- idea 到myeclipse
在上一篇博客使用maven进行开发过程管理之准备篇中提到了maven的基本概念.IT男罗书全觉得概念我是懂了,但是那些东西似乎离我很远啊.先开发再说吧, 于是IT男罗书全就在svn上取了源代码,并开始 ...
- js生成二维码实例(真实有效)
js文件 qrcode.js 代码 /*from tccdn minify at 2014-6-4 14:59:43,file:/cn/c/c/qrcode.js*/ /** * @fileov ...
- C++中的左值与右值(二)
以前以为自己把左值和右值已经弄清楚了,果然发现自己还是太年轻了,下面的这些东西是自己通过在网上拾人牙慧,加上自己的理解写的. 1. 2. 怎么区分左值和右值:知乎大神@顾露的回答. 3. 我们不能直接 ...
- Brocade,Cisco SAN Switch命令对比
- PHP 用html方式输出Excel文件时的数据格式设置
1) 文本:vnd.ms-excel.numberformat:@ 2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd 3) 数字:vnd.ms-excel.numb ...
- 关于Nios II的启动分析(转载)
原文地址:http://hi.baidu.com/goatdai/item/cc33671545d89243e75e06ad 常用到的存储器包括SDRMA.SRAM.FLASH.Onchip_memo ...
- Object转bigdecimal
/*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ import java.math.BigDecimal; //数字字符串 String StrBd=& ...