3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 102 Solved: 46
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 7
3 6
6 10
Sample Output
样例说明
奶牛1和奶牛3参与值班即可.
HINT
Source
题解:这道题做法有很多,比如:DP(虽然多半会TLE)、贪心、甚至最短路
DP:不用多说,就是通过各个区间进行对于前面的转移,所以需要用到一个快速的区间查询数据结构进行维护,但是很可惜,时限是1s,而T<=1000000,TLE是十有八九的事
贪心:显然,对于多个结束时间相同的区间来说,保留一个起始时间最长的即可,于是我们开始进行贪心
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ var
i,j,k,l,m,n,ans:longint;
a:array[..,..] of longint;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end; procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;x:=a[(l+r) div ,];y:=a[(l+r) div ,];
repeat
while (a[i,]<x) or ((a[i,]=x) and (a[i,]<y)) do inc(i);
while (a[j,]>x) or ((a[j,]=x) and (a[j,]>y)) do dec(j);
if i<=j then
begin
swap(a[i,],a[j,]);
swap(a[i,],a[j,]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
begin
readln(n,m);
a[,]:=;a[,]:=;
for i:= to n do readln(a[i,],a[i,]);
sort(,n);ans:=;
while l<m do
begin
j:=l;
while (k<n) and (a[k+,]<=(j+)) do
begin
inc(k);
l:=max(l,a[k,]);
end;
if (k=n) and (a[k,]<m) or (a[k+,]>(l+)) then
begin
writeln(-);
halt;
end;
inc(ans);
end;
if l<m then writeln(-) else writeln(ans);
end.
最短路:这道题个人认为最神奇的做法就是最短路,我们可以以每个时间点为节点,对于所有的(i,i-1)连边权为0的有向边,对于数据所给的时间段(x,y),连(x-1,y)边权为1的有向边,然后求0到T的最短路即可,还有——对于P党来说一个很大的重点在于离散化,必须把一些只指向前一个节点而且边权还为0的给压缩,这样子可以节省大量时间,我原来简单的最短路居然TLE,离散化之后就AC了(HansBug:感谢phile神犇的离散化点子么么哒)
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next:point;
end;
var
i,j,k,l,m,n,f,r:longint;
a,b:array[..,..] of longint;
c,g:array[..] of longint;
d:array[..] of longint;
e:array[..] of point;
p:point;
procedure add(x,y,z:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=e[x];e[x]:=p;
end;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;x:=b[(l+r) div ,];
repeat
while b[i,]<x do inc(i);
while b[j,]>x do dec(j);
if i<=j then
begin
swap(b[i,],b[j,]);
swap(b[i,],b[j,]);
swap(b[i,],b[j,]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end; begin
readln(n,m);
for i:= to n do
begin
readln(a[i,],a[i,]);a[i,]:=a[i,]-;
b[i*-,]:=a[i,];b[i*-,]:=i;b[i*-,]:=;
b[i*,]:=a[i,];b[i*,]:=i;b[i*,]:=;
end;
b[n*+,]:=m;b[n*+,]:=;b[n*+,]:=;
sort(,n*);
b[,]:=;
for i:= to n* do
begin
if b[i,]<>b[i-,] then inc(j);
a[b[i,],b[i,]]:=j;
end;
if b[n*+,]>b[n*,] then inc(j);
m:=j;
for i:= to m do e[i]:=nil;
for i:=m downto do add(i,i-,);
for i:= to n do add(a[i,],a[i,],);
fillchar(c,sizeof(c),);fillchar(g,sizeof(g),);
c[]:=;d[]:=;f:=;r:=;g[]:=;
while f<r do
begin
p:=e[d[f]];
while p<>nil do
begin
if (c[p^.g]=) or (c[p^.g]>(c[d[f]]+p^.w)) then
begin
c[p^.g]:=c[d[f]]+p^.w;
if g[p^.g]= then
begin
g[p^.g]:=;
d[r]:=p^.g;
inc(r);
end;
end;
p:=p^.next;
end;
inc(f);
g[d[f]]:=;
end;
for i:= to m do dec(c[i]);
writeln(c[m]);
readln;
end.
3389: [Usaco2004 Dec]Cleaning Shifts安排值班的更多相关文章
- Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 218 Solved: ...
- BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班
题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Description ...
- bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Description 一天有 ...
- 【BZOJ】3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3389 显然左端点排序后,依次取. 要考虑下一次取的方案: 待选点为a[j].x<=a[now] ...
- BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: ...
- bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班
思路:可以贪心,也可以最短路. 贪心写法:因为在保证合法的前提下,我们选择的区间一定要右端点尽量靠后才行,于是我们每次就选择一个合法的并且右端点最靠后的区间就好了(如果没有合法的输出-1即可).时间复 ...
- [bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路
Cleaning Shifts bzoj-3389 Usaco-2004Dec 题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班.有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求 ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 414 Solved: ...
随机推荐
- 在web前端使用SVG
前言: 花了些时间了解了一下svg,然而仍然不怎么了解... 第一步:直接在html代码中使用svg. 首先了解几个标签: <svg version="1.1" xmlns= ...
- windows下安装php5.2.*,php5.3.*,php5.4.*版本的memcache扩展
注:如使用集成环境成功率低,请自行配置php apache,表示win7下wamp php5.4.3基础上配置拓展,成功率极低.费时. 拓展安装调试方法: 编写调试php文件 <?php me ...
- JAVA轻量级文件监控
原文地址:http://blog.csdn.net/three_man/article/details/31012903?utm_source=tuicool 介绍 本文主要介绍一种轻量级的文件监控方 ...
- Apriori算法原理总结
Apriori算法是常用的用于挖掘出数据关联规则的算法,它用来找出数据值中频繁出现的数据集合,找出这些集合的模式有助于我们做一些决策.比如在常见的超市购物数据集,或者电商的网购数据集中,如果我们找到了 ...
- SuperSocket入门(二)- 探索AppServer、AppSession,Conmmand和App.config
在上一篇文章中,我们已经了解到了如何在SuperSocket处理客户端请求. 同时我们可能会发现一个问题,如果我们的服务器端包含有很多复杂的业务逻辑,这样的switch/case代码将会很 ...
- python中关于发邮件的示例
发送邮件示例代码如下: from WebUtils import ProperitiesLoad from email.mime.text import MIMEText from email.mim ...
- vue.js环境搭建
安装 nodejs 地址 :https://nodejs.org/en/ npm安装最新版本 更新npm :npm update -g 安装淘宝镜像 npm install -g cnpm --reg ...
- java cooki的使用
session: 当新客户端发现一个HTTP请求时服务端会创建一个session.并分配一个sessionID作为服务端来客户端的识别,session对象会 保存在服务端.此时session对象处天N ...
- C# out的使用 函数例题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Omi原理-环境搭建
环境搭建 Omi框架使用 Webpack + ES6 的方式去开发:使用karma+jasmine来作为Omi的测试工具. Karma介绍 Karma是一个基于Node.js的JavaScript测试 ...