思路:可以贪心,也可以最短路。

贪心写法:因为在保证合法的前提下,我们选择的区间一定要右端点尽量靠后才行,于是我们每次就选择一个合法的并且右端点最靠后的区间就好了(如果没有合法的输出-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安排值班的更多相关文章

  1. BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 45  Solved:  ...

  2. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

  3. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

  4. 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 102  Solved: ...

  5. bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description     一天有 ...

  6. 【BZOJ】3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3389 显然左端点排序后,依次取. 要考虑下一次取的方案: 待选点为a[j].x<=a[now] ...

  7. [bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路

    Cleaning Shifts bzoj-3389 Usaco-2004Dec 题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班.有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求 ...

  8. 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划

    [BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...

  9. BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

    1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 414  Solved: ...

随机推荐

  1. URAL 1780 G - Gray Code 找规律

    G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. CSS3/jQuery自己定义弹出窗体

    简单演示一下,精简了演示效果和css样式文件,更利于在项目中的实际应用 引入style.css   index.js <!DOCTYPE HTML PUBLIC "-//W3C//DT ...

  3. 对CAB文件进行数字签名

    对CAB文件进行数字签名 传说中数字签名之后就能够不出现提示而自己主动下载,所以也试试: 在\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin 中间有三个小 ...

  4. Redis 数据备份与恢复

    Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 redis 127.0.0.1: ...

  5. Linux 引导过程内幕

    转载:http://www.ibm.com/developerworks/cn/linux/l-linuxboot/index.html   从主引导记录到第一个用户空间应用程序的指导 引导 Linu ...

  6. Boxes in a Line

    Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...

  7. 安装DirectX SDK时出现Error Code:s1023 的解决方案

    刚刚安装DXSDK_Jun10时(下载地址:http://download.microsoft.com/download/A/E/7/AE743F1F-632B-4809-87A9-AA1BB3458 ...

  8. java通过反射获取调用变量以及方法

    一:反射概念 可以通过Class类获取某个类的成员变量以及方法,并且调用之. 二:通过反射获取方法.变量.构造方法 @Test // 通过反射获取类定义的方法 public void testMeth ...

  9. Adobe Edge Animate--关于全局变量和全局方法的定义

    Adobe Edge Animate--关于全局变量和全局方法的定义 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. BY:sonicxsxs 前文中有关音 ...

  10. (转载)ConcurrentHashMap 原理

    集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区 (Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章 ...