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 ...
随机推荐
- C#如何打开DBF数据库文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- php六种基础算法:冒泡,选择,插入,快速,归并和希尔排序法
$arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序法 * 思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. * 比 ...
- jQuery 根据JSON数据动态生成表格
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- oracle中SQL根据生日日期查询年龄的方法
方法:SELECT Trunc(MONTHS_BETWEEN(SYSDATE,BIRTH_DATE)/12) FROM 某表 Trunc函数在这里对带有小数位数的数字取整数部分: SYSDATE为or ...
- python生成器和迭代器
生成器:具有生成能力的对象 迭代器:具有取值功能的对象 def func(): yield 1 yield 2 yield 3 ret = func() #func()函数体称为生成器 r=ret._ ...
- js常用正则表达式2
字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个单词的边界. -或- 对 ...
- [转载] 3. JebAPI 之 jeb.api.ast
本文转载自: https://www.zybuluo.com/oro-oro/note/143651 0. 序 Jeb 本身是支持变量重命名的,所以,混淆了的变量名.类名可以修改. 实际上,它还可以做 ...
- [2015hdu多校联赛补题]hdu5372 Segment Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...
- AsyncTask的缺陷以及解决方法
1.AsyncTask常用于进行耗时操作,完成后更新主线程的UI. 2.缺陷:AsyncTask中维护着一个长度为128的线程池,同时可以执行5个工作线程,还有一个缓冲队列,当线程池中已有128个线程 ...
- highcharts 使用实例
后端使用django实现,返回的数据可以修改为从数据库获取或其他方式获取,实例里是写死的数据. urls配置: url(r'^outip/chart/$', views.charts), url(r' ...