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: ...
随机推荐
- Codeforces Round #313 (Div. 2) B. Gerald is into Art 水题
B. Gerald is into Art Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560 ...
- UOJ 08 Quine 是在下输了
#8. Quine Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/8 Description 写一个程序,使其能 ...
- Base64 图片转换工具
以前在写asp的后台的时候,有一个上传功能是必须的,那时候进行的图片预览(未上传前)其实就是获取本地的图片路径来显示图片,但是随着HTML5的出现,可以把图片通过编码来实现预览. 在雅虎的36条速度优 ...
- WWDC2015
- 第1章 游戏之乐——NIM(2)“拈”游戏分析
NIM(2)“拈”游戏分析 1. 问题 有N块石头和两个玩家A和B,玩家A先将石头分成若干堆,然后按照BABA……的顺序不断轮流取石头,能将剩下的石头一次取光的玩家获胜.每次取石头时,每个玩家只能从若 ...
- mysql导入数据库
mysql -u root -p bbs < d:\bbs_2011-06-15 --default-character-set=gbk mysqldump -uroot -p ta ...
- python学习笔记(三)--条件语句
Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条 ...
- Last non-zero Digit in N!
Problem Description The expression N!, read as "N factorial," denotes the product of the f ...
- hdfs: 数据流(二)
大部分的HDFS程序对文件操作需要的是一次写多次读取的操作模式. 一个文件一旦创建.写入.关闭之后就不需要修改了.这个假定简单化了数据一致的问题和并使高吞吐量的数据访问变得可能. 1. 读文件 从上图 ...
- [Oracle AR]Territory Flexfield
You can use the Territory Flexfield for recording and customized reporting on your territory informa ...