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 Gym 100286B Blind Walk DFS
Problem B. Blind WalkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/cont ...
- Meteor 加入账户系统
Meteor 加入账户系统 我们给meteor加入一个账户系统 导入包 meteor add ian:accounts-ui-bootstrap-3 meteor add accounts-passw ...
- Android 使用 Gmail 来发送邮件
Android 使用 Gmail 来发送邮件 1. [代码]SendMail.java package org.apache.android.mail; import android.app.Acti ...
- iOS开发UI-利用Quartz2D 实现基本绘图(画三角形、矩形、圆、圆弧)
1.画三角形 运行结果如下 1.1具体实现步骤 1.1.1首先新建一个project,然后自定义一个view 1.2代码 #import "htingShapeView.h" @ ...
- MYSQL源码 与 DBUG
一.前言 在规模稍微大点的项目中,为了方便快速找到bug的所在,我们往往需要在代码中加入一些调试用的代码,比如加入一些printf,打印出一些重点的信息:加入assert,进行断言判断.这些比较随意的 ...
- 高并发网络编程之epoll详解
select.poll和epoll的区别 在linux没有实现epoll事件驱动机制之前,我们一般选择用select或者poll等IO多路复用的方法来实现并发服务程序.在大数据.高并发.集群等一些名词 ...
- 如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
原文地址http://blog.sina.com.cn/s/blog_621c16b101013ygl.html OpenIntents项目和可用资源介绍 [1]. 项目介绍:OpenInten ...
- String StringBuilder以及StringBuffer
例一:[看了威哥视频,下面更好理解] package sunjava; public class String_test { public static void main(String[] args ...
- Divisibility
Description Consider an arbitrary sequence of integers. One can place + or - operators between integ ...
- iOS之duplicate symbols for architecture x86_64错误
在我们写代码过程中可能会经常遇到这样一个错误: <span style="font-size:32px;color:#ff0000;">ld: 4 duplicate ...