Problem

n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离。

Solution

用dp[i][j]表示前i个洞,进了前j个老鼠的最小代价

dp[i][j]=min(dp[i-1][k]+Sum[j]-Sum[k])(其中Sum[x]表示前x个老鼠到当前第i个洞的距离总和)

因此我们用单调队列维护dp[i-1][k]-Sum[k]

Notice

要开滚动数组,不然内存不够

Code

#include<deque>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int N = 5000;
const ll INF = 6e12;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int T[N + 5];
ll f[2][N + 5], Sum[N + 5];
deque<int> Q;
struct node
{
int p, c;
}H[N + 5];
int cmp(node X, node Y)
{
return X.p < Y.p;
}
int sqz()
{
int n = read(), m = read();
rep(i, 1, n) T[i] = read();
rep(i, 1, m) H[i].p = read(), H[i].c = read();
sort(T + 1, T + n + 1);
sort(H + 1, H + m + 1, cmp);
int now = 1, pre = 0;
rep(i, 0, n) f[pre][i] = INF;
f[pre][0] = 0;
rep(i, 1, m)
{
rep(j, 1, n) Sum[j] = Sum[j - 1] + abs(T[j] - H[i].p);
Q.clear();
Q.push_back(0);
rep(j, 1, n)
{
while (!Q.empty() && j - Q.front() > H[i].c) Q.pop_front();
while (!Q.empty() && f[pre][j] - Sum[j] <= f[pre][Q.back()] - Sum[Q.back()]) Q.pop_back();
Q.push_back(j);
f[now][j] = f[pre][Q.front()] - Sum[Q.front()] + Sum[j];
}
now ^= 1, pre ^= 1;
}
if (f[pre][n] == INF) puts("-1");
else printf("%I64d\n", f[pre][n]);
}

[Codeforces797F]Mice and Holes的更多相关文章

  1. [CodeForces-797F]Mice and Holes

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

  2. Codeforces 797 F Mice and Holes

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

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

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

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

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

  5. Mice and Holes CodeForces - 797F

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

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

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

  7. Mice and Holes

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

  8. Educational Codeforces Round 19

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

  9. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

随机推荐

  1. Asp.net core 学习笔记 ( User Secrets )

    参考 : http://cnblogs.com/nianming/p/7068253.html https://docs.microsoft.com/en-us/aspnet/core/securit ...

  2. CentOS 6.8 源码安装RabbitMQ

    一.安装依赖环境 yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ ...

  3. Server SQL Modes

    The MySQL server can operate in different SQL modes, and can apply these modes differently for diffe ...

  4. Sparksql的内置函数的使用以及案例

    开发环境:spark:2.2.0 工具:IDEA OS:Windows 数据文件: 001E8CB5AB11,ASUSTek,2018-07-12 14:00:57,2018-07-12 14:00: ...

  5. 基于react的记账簿开发

    前言 前端是纯 React,后端通过 axios 库请求服务器获得数据. 源码: https://github.com/hfpp2012/react-accounts-app 项目详解: https: ...

  6. string用scanf读入printf输出(节省时间)

    #include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

  7. Digital Deletions HDU - 1404

    Digital deletions is a two-player game. The rule of the game is as following. Begin by writing down ...

  8. 第二阶段——个人工作总结DAY04

    1.昨天做了什么:实现所有需要跳转活动的点击事件. 2.今天打算做什么:打算把值能够传递过去. 3.遇到的困难:无

  9. 论raw_input与input之间的缠缠绵绵

    例子1:py2.7中,raw_input输入整数,返回的是str. input1=raw_input("raw_input:") print(type(input1)) print ...

  10. 解决gitHub下载速度慢的问题

    转载:http://blog.csdn.net/x_studying/article/details/72588324 github被某个CDN被伟大的墙屏蔽所致. 解决方法: 1.访问http:// ...