【BZOJ】3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3389
显然左端点排序后,依次取。
要考虑下一次取的方案:
待选点为a[j].x<=a[now].y+1的所有点j,其中now是当前所选
那么我们要在这些点内做决策
贪心就是取y最大的待选点,即
max(a[j].y)的j
实现中细节太多QAQ,写了挺久的。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=25005;
int n, t, cnt;
struct dat { int x, y; }a[N];
bool cmp(const dat &a, const dat &b) { return a.x==b.x?a.y<b.y:a.x<b.x; } int main() {
read(n); read(t);
for1(i, 1, n) read(a[i].x), read(a[i].y);
sort(a+1, a+1+n, cmp);
a[n+1].x=100055050;
for1(i, 1, n) if(a[i].x!=a[i+1].x) a[++cnt]=a[i];
if(a[1].x>1) { puts("-1"); return 0; }
int ans=1, now=1;
for1(i, 1, cnt) {
int fix=0, nx=now, pos=0, ed=a[now].y;
while(nx<cnt && a[nx+1].x<=ed+1) {
++nx;
if(a[nx].y>ed && fix<a[nx].y) {
fix=a[nx].y;
pos=nx;
}
}
if(a[now].y==t) break;
if(nx==now || fix==0) { puts("-1"); return 0; }
now=pos;
i=nx;
++ans;
}
if(a[now].y!=t) puts("-1");
else print(ans);
return 0;
}
Description
Input
Output
Sample Input
1 7
3 6
6 10
Sample Output
样例说明
奶牛1和奶牛3参与值班即可.
HINT
Source
【BZOJ】3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)的更多相关文章
- 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安排值班
题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MB Description ...
- Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 218 Solved: ...
- 3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 102 Solved: ...
- BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班
3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: ...
- bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班
思路:可以贪心,也可以最短路. 贪心写法:因为在保证合法的前提下,我们选择的区间一定要右端点尽量靠后才行,于是我们每次就选择一个合法的并且右端点最靠后的区间就好了(如果没有合法的输出-1即可).时间复 ...
- BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- [bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路
Cleaning Shifts bzoj-3389 Usaco-2004Dec 题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班.有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求 ...
- bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚【dp+线段树】
设f[i]为i时刻最小花费 把牛按l升序排列,每头牛能用f[l[i]-1]+c[i]更新(l[i],r[i])的区间min,所以用线段树维护f,用排完序的每头牛来更新,最后查询E点即可 #includ ...
随机推荐
- php的正则表达式完全手册
前言 正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感.只要认真去阅读这些资料,加上应用的时候进行一定的参考,掌握正则表达式不是问题. 索引 1._引子 2. ...
- The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554
地址:http://acm.uestc.edu.cn/#/problem/show/1554 题目: C0ins Time Limit: 3000/1000MS (Java/Others) M ...
- Java应用开发中的SQL注入攻击
1. 什么是SQL注入攻击? SQL注入攻击是黑客对数据库进行攻击的常用手段之一.随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员越来越多.但是由于程序员的水平及经验参差不齐,相当一部分 ...
- 对java NIO 通道的一些了解
@引言 reactor(反应器)模式 使用单线程模拟多线程,提高资源利用率和程序的效率,增加系统吞吐量.下面例子比较形象的说明了什么是反应器模式: 一个老板经营一个饭店, 传统模式 - 来一个客人安排 ...
- linux下创建用户,给用户设置密码,给用户授权
1.linux下的用户是属于组的,所以需要创建一个组,划分给用户.创建命令: 在root下执行 groupadd ver 创建一个组ver 2.创建用户 useradd ...
- 在vi中打开多个文件,复制一个文件中多行到另一个文件中
:set number 查看行号1.vi a.txt b.txt或者vi *.txt 2.文件间切换 :n切换到下一个文件,:wn保存再切换 :N到上一个文件,:wN保存再切换 :.=看当前行 3.比 ...
- iframe自适应高度(兼容多种浏览器)
http://jingyan.baidu.com/article/b87fe19eaeb2cf5218356896.html 让iframe自适应高度,下面是实现的源码: <div id=&qu ...
- Java-性能调优工具-jstat
jps 查看当前java进程 [ ~]# jps 9939 Resin 9874 WatchdogManager 18293 Jps jstat -gc -t pid 1s [ ~]# jstat - ...
- spring cloud要点简介及常用组件
spring cloud基于spring boot spring cloud是通过包装其他技术框架实现的,例如OSS组件,实现了一套通过基于注解.java配置和基于模板开发的微服务框架. spring ...
- 20145322《Java程序设计》第十周学习总结
20145322<Java程序设计>第十周学习总结 教材学习内容总结 网络概述 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或 ...