POJ_2376_Cleaning Shifts【贪心】【区间覆盖】
题目大意:
有一些奶牛,每只奶牛负责一个时间段。问覆盖完全部的时间段最少需要多少只奶牛。若不能全部覆盖,输出-1.
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int maxd = + ; struct Edge
{
int x;
int y;
}cow[maxn]; int N, T;
int X, Y; bool cmp(Edge a, Edge b)
{
if (a.x == b.x) return a.y>b.y;
return a.x<b.x;
} void input()
{
X = ; Y = ;
scanf("%d%d", &N, &T);
for (int i = ; i<N; i++)
{
scanf("%d%d", &cow[i].x, &cow[i].y);
if (cow[i].x == ) X = ;
if (cow[i].y == T) Y = ;
}
} void solve()
{
sort(cow, cow + N, cmp);
//若所有的牛的工作时间的起始时间和结束时间都没有覆盖掉区间的起始时间和结束时间
if (X == || Y == )
{
printf("-1\n");
return;
}
int ans = , End = , sum = ;
int Start = cow[].y;
int maxy = cow[].y;
while (true)
{
while (End + <N&&cow[End + ].x <= Start + ) //End代表当前点的坐标
{
End++;
if (cow[End].y>maxy)
{
maxy = cow[End].y;
}
}
if (maxy != Start) //start为当前有效区间的最远距离
{
ans++;
Start = maxy;
}
else
{
if (End == N - ) //已覆盖掉区间
{
break;
}
else //说明中间有的时间点覆盖不到
{
printf("-1\n");
return;
}
}
}
printf("%d\n", ans);
return;
} int main() {
input();
solve();
return ;
}
2018-04-01
POJ_2376_Cleaning Shifts【贪心】【区间覆盖】的更多相关文章
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- Cleaning Shifts(区间覆盖)
/* http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1019&ojid=1&cid=10 题目: 给定一个时 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- git reset --hard xxxxxxx
关于git reset --hard xxxxxxx命令之Git版本回退 今晚代码写着写着就头脑有点发懵,手指也不听使唤了竟然让我敲出了 git reset --hard 命令,然后的然后就是之前所有 ...
- Confluence 6 为翻译显示用户界面的键(Key)名称
这个功能在你使用 Confluence 用户界面为 Confluence 创建翻译的时候会非常有用.当你打开主面板的时候,在你访问的 URL 的最后面添加下面的文字:can add the follo ...
- Android UiAutomator - CTS Frame
使用UiAutomator进行UI自动化测试后,生成的测试结果并不是很美观.为了生成一份好看的测试结果(报告),本文将使用CTS框架,当然也可以自己编写一份测试报告框架(如:生成html,excel报 ...
- Executor多线程框架使用
在我们的JDK1.5的时候JAVA推出一款为了更加方便开发的多线程应用而封装的框架(Executor),相比传统的Thread类,Executor更加的方便,性能好,更易于管理,而且支持线程池.一般在 ...
- SQLmap注入启发式检测算法
1.经过setTargetEnv()就进入了checkWaf()的环节 def checkWaf(): """ Reference: http://sec ...
- python接口自动化测试三十三:获取时间戳(10位和13位)
很多时候,在调用接口时,需要对请求进行签名.需要用到unix时间戳. 在python里,在网上介绍的很多方法,得到的时间戳是10位.而java里默认是13位(milliseconds,毫秒级的). 下 ...
- rpm 命令使用 和 lsof -p 1406 使用
#安装RPM -v 显示详细信息 -h 显示进度 -i 安装 -U 升级 -q 查询 -ql 查看rpm 包装的文件 - qf 查看命令属于哪个RPM 包 -qi 查看RPM包的详细信息 [root@ ...
- js中匿名函数和回调函数
匿名函数: 通过这种方式定义的函数:(没有名字的函数) 作用:当它不被赋值给变量单独使用的时候 1.将匿名函数作为参数传递给其他函数 2.定义某个匿名函数来执行某些一次性任务 var f = func ...
- OpenSSL + Windows 下载安装
Download Win32 OpenSSLhttps://slproweb.com/products/Win32OpenSSL.htmlhttps://wiki.openssl.org/index. ...
- SNMP MIBs and IPv6
https://www.cisco.com/c/en/us/about/security-center/snmp-mib-ipv6.html