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 ...
随机推荐
- 64位系统上使用PLSQL Dervloper解决方案
win7+64位+Oracle+11g+64位下使用PLSQL+Developer+的解决办法 2012-04-15 01:28:37| 分类: 默认分类 | 标签: |字号大中小 订阅 . w ...
- JavaScript 基础(七) 箭头函数 generator Date JSON
ES6 标准新增了一种新的函数: Arrow Function(箭头函数). x => x *x 上面的箭头相当于: function (x){ return x*x; } 箭头函数相当于匿名函 ...
- XidianOJ 1183 Water Problem: Items divided
题目描述 Youyouyouyou is very interested in math, one day, an idea came into his mind that how many ways ...
- -Dmaven.multiModuleProjectDirectory system propery is not set问题解决
-Dmaven.multiModuleProjectDirectory system propery is not set问题解决 eclipse中使用maven插件的时候,运行run as mave ...
- 【转载】OpenGL ES 三种类型修饰 uniform attribute varying
其实attribute varying已经被in和out代替了,但是有些工程代码里面仍然还在,所以权当笔记好了. 1.uniform变量uniform变量是外部application程序传递给(ver ...
- Log4j 配置 的webAppRootKey参数问题
为了让Web项目中的Spring 使用Log4j做如下配置: 1.在web.xml中添加如下内容: <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的&q ...
- 关于 this 和 prototype 的理解
1:this 的理解比较好的书是 <Javascript语言精粹> 平时我们全局写 var a = 1, 其实就是 window.a = 1; var f = function(){}, ...
- asp.net GDI+绘制多个矩形
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 我为什么选择使用Go语言?
谢孟军:EGO会员.GopherChina组织者.<Go Web编程>一书的作者,专注Golang技术架构.本文来自EGO会员群分享,入群方式见文末 在这里我主要想和大家分享一些Go和我个 ...
- ajax无刷新上传图片
页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...