题目大意:
  在一条直线上,有n个老鼠,m个洞。
  每个老鼠i都有一个初始位置x[i]。
  每个洞i都有一个固定位置p[i]和容量限制c[i]。
  求所有老鼠都进洞的最小距离总和。

思路:
  动态规划。
  用f[i][j]表示前i个洞、前j只老鼠的最小距离总和。
  用sum[i][j]表示前j个老鼠都进入第i个洞的距离总和。
  可以得到以下DP方程:
  f[i][j]=min{f[i-1][k]-sum[i][k]|k<=j}+sum[i][j]。
  然后就MLE,发现sum可以每次求出来,f如果倒着推,也可以省掉一维。
  这样空间复杂度就是O(n)的,时间复杂度是O(n^2m)的,在第42个点TLE了。
  考虑使用单调队列维护f[i-1][k]-sum[i][k]的min,做到O(nm)。

 #include<deque>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) if(ch=='-') neg=true;
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return neg?-x:x;
}
const long long inf=0x7fffffffffffffffll;
const int N=;
int x[N];
struct Hole {
int p,c;
bool operator < (const Hole &another) const {
return p<another.p;
}
};
Hole h[N];
long long f[][N],sum[N];
std::deque<int> q;
int main() {
int n=getint(),m=getint();
for(register int i=;i<=n;i++) {
x[i]=getint();
}
for(register int i=;i<=m;i++) {
h[i]=(Hole){getint(),getint()};
}
std::sort(&x[],&x[n+]);
std::sort(&h[],&h[m+]);
std::fill(&f[][],&f[][n+],inf);
for(register int i=;i<=m;i++) {
for(register int j=;j<=n;j++) {
sum[j]=sum[j-]+std::abs(h[i].p-x[j]);
}
q.clear();
q.push_back();
for(register int j=;j<=n;j++) {
while(!q.empty()&&j-q.front()>h[i].c) {
q.pop_front();
}
while(!q.empty()&&f[!(i&)][j]-sum[j]<=f[!(i&)][q.back()]-sum[q.back()]) {
q.pop_back();
}
q.push_back(j);
f[i&][j]=f[!(i&)][q.front()]+sum[j]-sum[q.front()];
}
}
printf("%I64d\n",f[m&][n]!=inf?f[m&][n]:-);
return ;
}

[CodeForces-797F]Mice and Holes的更多相关文章

  1. AC日记——Mice and Holes codeforces 797f

    797F - Mice and Holes 思路: XXYXX: 代码: #include <cmath> #include <cstdio> #include <cst ...

  2. Mice and Holes CodeForces - 797F

    Mice and Holes CodeForces - 797F 题意:有n只老鼠和m个洞,都在一个数轴上,老鼠坐标为x[1],...,x[n],洞的坐标为p[1],...,p[m],每个洞能容纳的老 ...

  3. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  4. Mice and Holes 单调队列优化dp

    Mice and Holes 单调队列优化dp n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离.1 ≤ n, m ≤ 5000. ​ 首先列出朴素的dp方程:\(f[i][j] ...

  5. CF797F Mice and Holes 贪心、栈维护DP

    传送门 首先\(\sum c\)有些大,考虑将其缩小降低难度 考虑一个贪心:第一次所有老鼠都进入其左边第一个容量未满的洞(如果左边没有就进入右边第一个未满的洞),第二次所有老鼠都进入其右边第一个容量未 ...

  6. Codeforces 793C - Mice problem(几何)

    题目链接:http://codeforces.com/problemset/problem/793/C 题目大意:给你一个捕鼠器坐标,和各个老鼠的的坐标以及相应坐标的移动速度,问你是否存在一个时间点可 ...

  7. [Codeforces797F]Mice and Holes

    Problem n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离. Solution 用dp[i][j]表示前i个洞,进了前j个老鼠的最小代价 dp[i][j]=min(dp[i ...

  8. Mice and Holes

    题意: 有 $n$ 只老鼠和 $m$ 个鼠洞,第 $i$ 只老鼠的坐标为 $x_i$,第 $j$ 个鼠洞的坐标为 $p_j$ ,容量为 $c_j$. 第 $i$ 只老鼠钻进第 $j$ 个鼠洞的距离为 ...

  9. Educational Codeforces Round 19

    A. k-Factorization 题目大意:给一个数n,求k个大于1的数,乘积为n.(n<=100,000,k<=20) 思路:分解质因数呗 #include<cstdio> ...

随机推荐

  1. TinyOS 代码分析

    1.Basestation案例   位于/opt/tinyos-main-master/apps/Basetation 1.1本例的顶层结构图: 1.2软件实现流程 1) uartIn,uartOut ...

  2. 72.Edit Distance---dp

    题目链接:https://leetcode.com/problems/edit-distance/description/ 题目大意:找出两个字符串之间的编辑距离(每次变化都只消耗一步). 法一(借鉴 ...

  3. python之smtplib库学习

    # -*- coding:utf-8 -*- import smtplibfrom email.mime.text import MIMETextfrom email import encodersf ...

  4. 《深入理解Java虚拟机》笔记--第二章、Java内存区域与内存溢出异常

    Java程序员把内存的控制权交给了Java虚拟机.在Java虚拟机内存管理机制的帮助下,程序员不再需要为每一个new操作写对应的delete/free代码,而且不容易出现内存泄露和溢出. 虚拟机在执行 ...

  5. 手机页面或是APP中减少使用setTimeout和setInterval,因为他们会导致页面卡顿

    1.setTimeout致使页面的卡顿或是不流畅,打乱模块的生命周期 ,还有setTimeout其实是很难调试的. 当一个页面有众多js代码的时候,setTimeout就是导致页面的卡顿. var s ...

  6. [ python ] 初始面向对象

    首先,通过之前学习的函数编写一个 人狗大战 的例子. 分析下这个需求,人 狗 大战  三个事情.角色:人.狗动作:狗咬人,人打狗 先创建人和狗两个角色: def person(name, hp, ag ...

  7. ntp 校时程序

    //effect:send ntp packet and get the ntp packet ,make the time OK//2014.7.31 is OK//#include <sys ...

  8. Linux Shell基础篇——变量

    一.Shell中的变量 注:这里所说的Shell是Bash Shell,我姑且统称为Shell. Shell中的变量分为用户自定义变量.环境变量.位置参数变量.预定义变量.在Shell中,变量的默认类 ...

  9. GO基本数据结构练习:数组,切片,映射

    按<GO IN ACTION>的书上进行. 应该是第二次了哦~~ package main import ( "fmt" ) func main() { array : ...

  10. jsonp原生js跨域拿新浪数据插件封装【可扩展】

    //修改了一个bug,增加了手动释放垃圾 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...