[CodeForces-797F]Mice and Holes
题目大意:
在一条直线上,有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的更多相关文章
- AC日记——Mice and Holes codeforces 797f
797F - Mice and Holes 思路: XXYXX: 代码: #include <cmath> #include <cstdio> #include <cst ...
- Mice and Holes CodeForces - 797F
Mice and Holes CodeForces - 797F 题意:有n只老鼠和m个洞,都在一个数轴上,老鼠坐标为x[1],...,x[n],洞的坐标为p[1],...,p[m],每个洞能容纳的老 ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- Mice and Holes 单调队列优化dp
Mice and Holes 单调队列优化dp n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离.1 ≤ n, m ≤ 5000. 首先列出朴素的dp方程:\(f[i][j] ...
- CF797F Mice and Holes 贪心、栈维护DP
传送门 首先\(\sum c\)有些大,考虑将其缩小降低难度 考虑一个贪心:第一次所有老鼠都进入其左边第一个容量未满的洞(如果左边没有就进入右边第一个未满的洞),第二次所有老鼠都进入其右边第一个容量未 ...
- Codeforces 793C - Mice problem(几何)
题目链接:http://codeforces.com/problemset/problem/793/C 题目大意:给你一个捕鼠器坐标,和各个老鼠的的坐标以及相应坐标的移动速度,问你是否存在一个时间点可 ...
- [Codeforces797F]Mice and Holes
Problem n个老鼠,m个洞,告诉你他们的一维坐标和m个洞的容量限制,问最小总距离. Solution 用dp[i][j]表示前i个洞,进了前j个老鼠的最小代价 dp[i][j]=min(dp[i ...
- 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> ...
随机推荐
- 在C++11中实现监听者模式
参考文章:https://coderwall.com/p/u4w9ra/implementing-signals-in-c-11 最近在完成C++大作业时,碰到了监听者模式的需求. 尽管C++下也可以 ...
- HDU 1045 Fire Net (深搜)
题目链接 Problem DescriptionSuppose that we have a square city with straight streets. A map of a city is ...
- win32的回调函数
[转]http://blog.csdn.net/w419675647/article/details/6599070 众所周知,win32的回调函数WndProc()是操作系统调用的函数,win32用 ...
- URAL题解一
URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...
- centos7安装完成后的一些配置
1.打开终端 输入 sudo yum -y update 先更新软件包 2.这是输入语言 应用程序->系统工具->设置->区域和语言->+ ->汉语(中国)-> ...
- 处理tomcat内存溢出问题
TOMCAT起步内存溢出问题Exception in thread ""http-bio-8080"-exec-java.lang.OutOfMemoryError: P ...
- java.lang.IllegalArgumentException: Page directive: invalid value for import
我的项目原来用的tomcat版本是apache-tomcat-7.0.53,后来为了安全原因将版本升至\apache-tomcat-7.0.57,发现有的jsp页面出现下面的异常: java.lang ...
- HTML文件编码
为了防止中文乱码,一般在网页头文件中加入 <meta http-equiv="Content-Type" content="text/html; charset=u ...
- IDEA配置toString方法
1.toString JSON带父类toString public java.lang.String toString() { final java.lang.StringBuilder sb = n ...
- (五)Spring 对事务的支持
第一节:事务简介 满足一下四个条件: 第一:原子性: 第二:一致性: 第三:隔离性: 第四:持久性: ------------------------------------------------- ...