[Codeforces797F]Mice and Holes
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的更多相关文章
- [CodeForces-797F]Mice and Holes
题目大意: 在一条直线上,有n个老鼠,m个洞. 每个老鼠i都有一个初始位置x[i]. 每个洞i都有一个固定位置p[i]和容量限制c[i]. 求所有老鼠都进洞的最小距离总和. 思路: 动态规划. 用f[ ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- AC日记——Mice and Holes codeforces 797f
797F - Mice and Holes 思路: XXYXX: 代码: #include <cmath> #include <cstdio> #include <cst ...
- Mice and Holes 单调队列优化dp
Mice and Holes 单调队列优化dp n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离.1 ≤ n, m ≤ 5000. 首先列出朴素的dp方程:\(f[i][j] ...
- Mice and Holes CodeForces - 797F
Mice and Holes CodeForces - 797F 题意:有n只老鼠和m个洞,都在一个数轴上,老鼠坐标为x[1],...,x[n],洞的坐标为p[1],...,p[m],每个洞能容纳的老 ...
- CF797F Mice and Holes 贪心、栈维护DP
传送门 首先\(\sum c\)有些大,考虑将其缩小降低难度 考虑一个贪心:第一次所有老鼠都进入其左边第一个容量未满的洞(如果左边没有就进入右边第一个未满的洞),第二次所有老鼠都进入其右边第一个容量未 ...
- Mice and Holes
题意: 有 $n$ 只老鼠和 $m$ 个鼠洞,第 $i$ 只老鼠的坐标为 $x_i$,第 $j$ 个鼠洞的坐标为 $p_j$ ,容量为 $c_j$. 第 $i$ 只老鼠钻进第 $j$ 个鼠洞的距离为 ...
- Educational Codeforces Round 19
A. k-Factorization 题目大意:给一个数n,求k个大于1的数,乘积为n.(n<=100,000,k<=20) 思路:分解质因数呗 #include<cstdio> ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
随机推荐
- linux c/c++ 获取文件大小
linux c/c++ 获取文件大小 #include <sys/stat.h> int FileSize(const char* fname) { struct stat statbuf ...
- python + lisp hy的新手注记1
想在python里用lisp方言hy的目的: 1 用lisp去parse 包含 “数据+简单if控制流(代码.AST)”的配置文件,或者说用包含s-exp的.hy文件作为这类配置文件的实现(而不是用y ...
- Golang atomic
原子操作函数 分为下面系列函数,其中Xxx可以是Int32/Int64/Uint32/Uint64/Uintptr/Pointer其中一种. 1.SwapXxx系列:交换新旧值: // SwapInt ...
- Day1-Request/BeautifulSoup
requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...
- (转)3个常用基于Linux系统命令行WEB网站浏览工具(w3m/Links/Lynx)
一般我们常用的浏览器肯定是基于可视化界面的图文结合的浏览界面效果,比如FireFox.Chrome.Opera等等,但是有些时候折腾和项目 的需要,在Linux环境中需要查看某个页面的文字字符,我们需 ...
- 混合线性模型(linear mixed models)
一般线性模型.混合线性模型.广义线性模型 广义线性模型GLM很简单,举个例子,药物的疗效和服用药物的剂量有关.这个相关性可能是多种多样的,可能是简单线性关系(发烧时吃一片药退烧0.1度,两片药退烧0. ...
- gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i'
2017-12-13 10:44:19gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i' 1.3.100 driver/char/random.cst ...
- 封装jsonp
1.写一个类封装jsonp: jsonp(url, params, success, funName) 参数url:请求地址 参数params:请求数据,可以是json对象,或形如&q ...
- centos7 keepalived+nginx实现vip漂移高可用
一.Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前 ...
- sqlserver创建计算列 转
转 http://www.cnblogs.com/lgx5/p/6017874.html 表中其它列的计算值 创建的sql create table table1 ( number decimal(1 ...