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有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...
随机推荐
- PHP7 新特性 简介
整理了一些常用的新特性,欢迎点赞!!! 新增操作符 1.?? $username = $_GET['user'] ?? ''; $username = isset($_GET['user']) ? $ ...
- js 根据当前星期做跳转(代码段)
var week = [6,0,1,2,3,4,5]; $('.HotShop_head .HotShop_tab:eq('+week[new Date().getDay()]+')').click( ...
- php入门变量之数字
在介绍变量时,我明确指出PHP具有整型和浮点型(小数)数字类型.但是,依据我的经验,这两种类型都可以归类到一般的数字之下(在极大程度上是这样的). 下面列举下PHP中有效的数字类型的变量: 8 3.1 ...
- hibernate的n+1问题
下面选自<精通Hibernate:Java对象持久化技术详解>作者:孙卫琴 在Session的缓存中存放的是相互关联的对象图.默认情况下,当Hibernate从数据库中加载Customer ...
- 别让emacs损伤你的小母指
刚接触emacs时感觉,这东西怎么这么难用,还说是编辑器的神,我去. 写个代码跟挫游戏机手柄似的,关键是还这么难挫,平时用的最多的左ctrl键,这么难按,可怜的我的小母指(Petyr Baelish) ...
- gridview 一个列勾选框选中,同时选中同一行的另一列勾选框
<asp:TemplateColumn > <HeaderTemplate> 是否显示 <asp:CheckBox ID="chk_Show" sty ...
- GridView ItemCommand
GridView ItemCommand中取某行某列的值方法,这里提供两个常用的: 一.用CommandArgument属性取值页面如下: <asp:TemplateColumn HeaderT ...
- 【BZOJ 3098】 Hash Killer II
Description 这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题:给你一个长度为N的字符串S,求有多少个不同的长度为L的子串.子串的定义是S[l].S[l + 1].... S ...
- DB天气app冲刺二阶段第一天
原来找人也是个力气活...好费劲呀..今天的进度有点慢,,确切的说是没有什么进度 因为不会弄了..加上今天一个劲的找同学帮忙写评论.心思没定下来 根本没思路了..明天按照今天的计划继续冲刺..
- 总有你需要的之 ios 小技巧 (下)
图片上绘制文字 写一个UIImage的category NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultPara ...