POJ2376-Cleaning Shifts
2016-04-05 17:36
288 查看
在t个shift,n头牛中选择用最少的牛完成所有的shift。
明显是个贪心的题。
只要每次找出最晚结束shift的牛即可找到最优解。
加油。
明显是个贪心的题。
只要每次找出最晚结束shift的牛即可找到最优解。
#include <cstdio> #include <utility> #include <algorithm> using namespace std; const int maxn = 25000; typedef pair<int, int> cow; cow itv[maxn+2]; int comp(cow a, cow b) { if(a.first == b.first) { return a.second > b.second; } return a.first < b.first; } int main() { int n, t; scanf("%d%d", &n, &t); int max_end = 0; for (int i = 0; i < n; i++) { scanf("%d%d", &itv[i].first, &itv[i].second); max_end = max(max_end, itv[i].second); } sort(itv, itv + n, comp); if (max_end < t || itv[0].first != 1) { printf("-1\n"); return 0; } int cnt = 1; int mark = 0; int i = 0; int cur = itv[0].second; while (cur < t) { bool flag = false; for (i = mark; i < n && itv[i].first <= cur + 1; i++) { if (itv[i].second > itv[mark].second) { flag = true; mark = i; } } if (!flag) { cnt = -1; break; } cur = itv[mark].second; cnt++; } printf("%d\n", cnt); return 0; }
加油。
相关文章推荐
- marathon参考(3)服务发现和负载均衡
- 可重入概念
- 线性表的应用1(顺序表的遍历和查找)
- centos下安装php
- mysql5中大数据错误:Packet for query is too large (1117260 > 1048576). You can change this value on the ser
- Elementary0.3.2/Ubuntu14.04安装GooglePinYin输入法
- 判断字符串回环变位
- Python3.5入门学习记录-条件控制
- linux远程命令行安装matlab
- Android音视频录制类MediaRecorder用法举例
- 将UIDatePicker的日期显示为数字和中文
- 如何创建新的FreeRTOS工程
- Eclipse 重构
- 10gRAC修改vip,public ip
- 《剑指Offer》学习笔记——从尾到头打印链表
- jsp分页显示Java代码实现
- 函数指针和函数对象
- 查找不在数组里的字母
- Nodejs in Visual Studio Code 03.学习Express
- get-pip.py 安装