bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班
思路:可以贪心,也可以最短路。
贪心写法:因为在保证合法的前提下,我们选择的区间一定要右端点尽量靠后才行,于是我们每次就选择一个合法的并且右端点最靠后的区间就好了(如果没有合法的输出-1即可)。时间复杂度O(nlogn)(排序是nlogn的,贪心是O(n)的)。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 25005 int n,t,ans;
int last[1000005]; struct node{
int l,r;
bool operator <(const node &a)const{return l<a.l||(l==a.l&&r<a.r);}
}a[maxn]; inline int read(){
int x=0;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar());
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x;
} int main(){
n=read(),t=read();
for (int i=1;i<=n;i++) a[i].l=read(),a[i].r=read();
sort(a+1,a+n+1);int cnt=0;
for (int i=1;i<=n;i++)
if (a[i].l!=a[i+1].l) a[++cnt]=a[i];
n=cnt;int now=0;
for (int i=1;i<=n;i++){
int x=0;bool flag=0;
while (a[i].l<=now+1&&i<=n) x=max(x,a[i].r),i++,flag=1;
if (!flag){ans=-1;break;}
if (x>now) now=x,ans++;
i--;
}
if (now!=t) ans=-1;
printf("%d\n",ans);
return 0;
}
最短路写法:区间[l,r]表示可以从l-1走到r,那么我们就把l-1连一条权值为1的边到r即可,然后又因为区间可以有交集,所以还需要将i向i-1连一条权值为0的边,然后以0为起点跑最短路即可(以0为起点是因为是l-1连向r)。时间复杂度O(TlogT)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define maxn 1000005
#define inf 1e9 int n,t,tot;
int now[maxn],pre[maxn*2],son[maxn*2],val[maxn*2],dis[maxn];
bool vis[maxn]; inline int read(){
int x=0;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar());
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x;
} void add(int a,int b,int c){
son[++tot]=b;
pre[tot]=now[a];
now[a]=tot;
val[tot]=c;
} void link(int a,int b,int c){
add(a,b,c);
} struct node{
int id,val;
node(){}
node(int a,int b){id=a,val=b;}
bool operator <(const node &a)const{return val>a.val;}
};
priority_queue<node> heap; void dijkstra(int x){
memset(dis,127,sizeof(dis)),dis[x]=0;
heap.push(node(x,0));
while (!heap.empty()){
node x=heap.top();heap.pop();int id=x.id,v=x.val;
if (vis[id]) continue;vis[id]=1;
for (int p=now[id];p;p=pre[p])
if (dis[son[p]]>v+val[p]) heap.push(node(son[p],dis[son[p]]=v+val[p]));
}
} int main(){
n=read(),t=read();
for (int i=1,a,b;i<=n;i++) a=read(),b=read(),link(a-1,b,1);
for (int i=1;i<=t;i++) link(i,i-1,0);
dijkstra(0);
if (dis[t]>1e9) puts("-1");else printf("%d\n",dis[t]);
return 0;
}
bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班的更多相关文章
- BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: ...
- 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 ...
- 3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 102 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安排值班(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3389 显然左端点排序后,依次取. 要考虑下一次取的方案: 待选点为a[j].x<=a[now] ...
- [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: ...
随机推荐
- U盘安装Win7 64位系统(笔记本+台式机亲测)
准备工具: 1. Win7 64位系统的镜像文件(网上随便一搜即可,最好是纯净版,没有一堆乱七八糟的内置软件) 2. 4G以上的U盘一个 所用软件: 老毛桃(官网下载) 具体步骤: 1.数据备份(将原 ...
- Cache和Buffer的区别
一.研究数据库的人这样理解:http://wenku.baidu.com/view/32b8b13e376baf1ffc4fad7e.html Cache和Buffer是两个不同的概念,简单的说,Ca ...
- C#并行编程 (Barrier,CountdownEvent,ManualResetEventSlim,SemaphoreSlim,SpinLock,SpinWait )
背景 有时候必须访问变量.实例.方法.属性或者结构体,而这些并没有准备好用于并发访问,或者有时候需要执行部分代码,而这些代码必须单独运行,这是不得不通过将任务分解的方式让它们独立运行. 当任务和线程要 ...
- 强烈推荐240多个jQuery插件提供下载
jQuery 是继 prototype 之后又一个优秀的 Javascript 框架.其宗旨是—写更少的代码,做更多的事情.它是轻量级的 js 库(压缩后只有21k) ,这是其它的 js 库所不及 的 ...
- Redis 有序集合(sorted set)
Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合中的成员进行从小到大的排序. 有序 ...
- VBA-工程-找不到工程或库-解决方案
近来,越来越多的朋友被“找不到工程或库”的错误所烦恼,所以决定新开一帖来聊聊此问题! QUOTE: 一般情况下,出现此错误是因为找不到引用工程,或找不到与工程语言对应的引用的对象库 出现此类错误可以根 ...
- Android进阶笔记16:ListView篇之ListView刷新显示(全局 和 局部)
一.ListView内容变化后,动态刷新的步骤(全局刷新): (1)更新适配器Adapter数据源:(不要使用匿名内部类) (2)调用适配器Adapter的刷新方法notifyDataSetChang ...
- 读取文件txt
/// <summary> /// 读取文件 /// </summary> /// <param name="path ...
- ubuntu14_gtk 安装
1:apt-get install build-essential2:apt-get install gnome-devel gnome-devel-docs
- DedeCMS更新文章同步发布到新浪微博
如果在网站推广过程中能利用好微博这个工具的话,将会给网站的推广工作带来巨大的便利.下面以dede程序为例讲讲如何将网站内容自动同步到新浪微博. 在新浪微博的工具中有个自动关联博客的功能,利用好这个功能 ...