poj2373
其实这道题不是很难,不难想到f[i]表示覆盖到[0,i]的最少喷头数
很明显是一个dp+单调队列的问题
但是细节问题比较多,首先是不能覆盖到[0,l]外面,所以长度为奇数不能被完全覆盖
还有一些区间[bi,ei]只能被一个喷头覆盖,这意味着[0,s],bi<s<ei也是不能被完全覆盖的
标记一下就好了
最后注意判断一下是否可行
const inf=;
type node=record
x,y:longint;
end;
var f,q:array[..] of longint;
can:array[..] of boolean;
a,b:array[..] of longint;
p,n,l,w,v,i,j,h,t,ans:longint; procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) shr ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
swap(b[i],b[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; procedure merge;
var l,r:longint;
begin
l:=a[];
r:=b[];
t:=;
for i:= to n do
begin
if a[i]<r then r:=max(r,b[i])
else begin
if r-l>v* then
begin
writeln(-);
halt;
end;
inc(t);
a[t]:=l;
b[t]:=r;
l:=a[i];
r:=b[i];
end;
end;
if r-l>v* then
begin
writeln(-);
halt;
end;
inc(t);
a[t]:=l;
b[t]:=r;
end; begin
readln(n,l);
readln(w,v);
for i:= to n do
readln(a[i],b[i]);
sort(,n);
merge;
for i:= to t do
for j:=a[i]+ to b[i]- do
can[j]:=true;
f[]:=;
h:=;
t:=;
for i:= to l do
begin
f[i]:=inf;
p:=i-*w;
if (p>=) then
begin
while (t>) and (h<t) and (f[q[t-]]>=f[p]) do dec(t);
if not can[p] then
begin
q[t]:=p;
inc(t);
end;
end;
if (can[i]) or (i mod =) then continue;
while (h<t) and (q[h]<i-*v) do inc(h);
if (h<t) then f[i]:=f[q[h]]+;
end;
if f[l]>=inf then writeln(-) else writeln(f[l]);
end.
const inf=;
type node=record
x,y:longint;
end;
var f,q:array[..] of longint;
can:array[..] of boolean;
a,b:array[..] of longint;
p,n,l,w,v,i,j,h,t,ans:longint; procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) shr ];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
swap(b[i],b[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; procedure merge;
var l,r:longint;
begin
l:=a[];
r:=b[];
t:=;
for i:= to n do
begin
if a[i]<r then r:=max(r,b[i])
else begin
if r-l>v* then
begin
writeln(-);
halt;
end;
inc(t);
a[t]:=l;
b[t]:=r;
l:=a[i];
r:=b[i];
end;
end;
if r-l>v* then
begin
writeln(-);
halt;
end;
inc(t);
a[t]:=l;
b[t]:=r;
end; begin
readln(n,l);
readln(w,v);
for i:= to n do
readln(a[i],b[i]);
sort(,n);
merge;
for i:= to t do
for j:=a[i]+ to b[i]- do
can[j]:=true;
f[]:=;
h:=;
t:=;
for i:= to l do
begin
f[i]:=inf;
p:=i-*w;
if (p>=) then
begin
while (t>) and (h<t) and (f[q[t-]]>=f[p]) do dec(t);
if not can[p] then
begin
q[t]:=p;
inc(t);
end;
end;
if (can[i]) or (i mod =) then continue;
while (h<t) and (q[h]<i-*v) do inc(h);
if (h<t) then f[i]:=f[q[h]]+;
end;
if f[l]>=inf then writeln(-) else writeln(f[l]);
end.
poj2373的更多相关文章
- [USACO2004][poj2373]Dividing the Path(DP+单调队列)
http://poj.org/problem?id=2373 题意:一条直线分割成N(<=25000)块田,有一群奶牛会在其固定区域吃草,每1把雨伞可以遮住向左右延伸各A到B的区域,一只奶牛吃草 ...
- poj2373 Dividing the Path (单调队列+dp)
题意:给一个长度为L的线段,把它分成一些份,其中每份的长度∈[2A,2B]且为偶数,而且不能在某一些区间内部切开,求最小要分成几份 设f[i]为在i处切一刀,前面的满足要求的最小份数,则f[L]为答案 ...
- poj2373 Dividing the Path
Dividing the Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5060 Accepted: 1782 ...
- DP总结 ——QPH
常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...
随机推荐
- [Linux]学习笔记(3)-uname的用法
uname的用法如下: uname –a[--all]:输出全部信息 [root@linuxforlijiaman ~]# uname -a Linux linuxforlijiaman -.el6. ...
- 深入理解php的MySQL连接类
php的MySQL连接类. 后面几个show_databases和show_tables....等方法都用了一堆echo,好像一直不喜欢在类的方法里直接用输出语句,不过这也只是列举数据库和表名,构造 ...
- Python时间和日期学习
#coding=utf-8 __author__ = 'Administrator' #日期和时间模块学习 """ Python程序能用很多方式处理日期和时间,转换日期格 ...
- 解决pxe网络批量安装部署linux遇到的问题和解决方法
解决“出现Unable to retrieve 192.168.0.100/var/www/html/images/install.img错误” 分析:我们必须了解这个错误出现在哪个阶段才能正确找到错 ...
- javascript看你能够做对几题
http://ourjs.com/detail/52fb82e13bd19c4814000001
- 快捷设置IE代理小工具
时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...
- C语言基础(转载自大海笔记)
# C语言基础2015年03月26日10:04:411. 语言排行榜C——java——objective-C2. 进制:进制:进位机制.用普通的话讲,应该为人为的定义一种度量来标识一样东西 ...
- Understanding Responsive Web Design: Cross-browser Compatibility
http://www.sitepoint.com/understanding-responsive-web-design-cross-browser-compatibility/ In the las ...
- hive-site.xml 参数设置
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...
- 高性能图文混排框架,构架顺滑的iOS应用-b
About GallopGallop是一个功能强大.性能优秀的图文混排框架. Features主要用于解决以下需求: 滚动列表的性能优化.Gallop使用异步绘制.视图层级合并.观察mainRunlo ...