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: ...
随机推荐
- CDOJ 481 Apparent Magnitude 水题
Apparent Magnitude Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/sh ...
- Hanoi Tower问题分析
前言 回家休息第3天了,状态一直不是太好,主要是要补牙,检查身体,见同学见亲戚,心里又着急校招,难得能腾出时间来好好思考,这里也是看<cracking the coding interview& ...
- Spark Core源代码分析: Spark任务运行模型
DAGScheduler 面向stage的调度层,为job生成以stage组成的DAG,提交TaskSet给TaskScheduler运行. 每个Stage内,都是独立的tasks,他们共同运行同一个 ...
- truncate 与 delete 的区别
Delete删除的数据可以通过日志文件进行恢复 Truncate Table删除的数据不能进行恢复 Delete删除时,标识列取值保留原使用中最大值 Truncate Table删除时,标识列恢复到最 ...
- 03 InnoDB锁问题
InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...
- mysql中使用count()统计的特殊之处
如果你的需要是统计总行数时,为什么要使用count(*),而避免使用指定具体的列名? count()函数里面的参数是列名的的时候,那么会计算有值项的次数.也就是,该列没有值的项并不会进入计算范围.这样 ...
- The 7 Stages Of Scaling Web Apps--reference
reference from:http://highscalability.com/7-stages-scaling-web-apps TUESDAY, SEPTEMBER 23, 2008 AT 4 ...
- C++/CLR Sqlite初探
error C2491: "acosh": 不允许 dllimport 函数 的定义 asinh": 不允许 dllimport 函数 的定义 a ...
- cocos2d-x, protobuf, no config.h, #error "No suitable threading library available."
在用cocos2d-x3.2 + protobuf编译Android项目的时候,protobuf出现了两个问题: 1. 首先是config.h找不到,查阅自后说是通过命令或工具生成的,里面的内容根据不 ...
- Android(java)学习笔记81:java异常处理机制
1.try....catch/try...catch...finally package cn.itcast_02; /* * 我们自己如何处理异常呢? * A:try...catch...final ...