POJ 2376 Cleaning Shifts(贪心)
来源:互联网 发布:于慧英语怎么样知乎 编辑:程序博客网 时间:2024/04/19 11:58
http://poj.org/problem?id=2376
题意:有n头牛,t件衣服,每头牛可以洗某个区间的衣服,要保证所有衣服均被洗过一次,问最少需要几头牛。无解输出-1.
解法:很容易想到贪心,直接将每头牛的起始位置和终止位置记录下来,排序,从前往后走。
需要注意的细节,每次贪心完后取得一个最远的坐标Max,下次找在Max里面的应该是寻找那些满足起始位置小于等于Max+1的牛。比如1 5,6 10,这两头牛就可以把1到10的衣服全洗完。在这个情况下又要记得,不要被这个+1搞混了,我们当前的坐标end和Max是两个概念,Max+1能到达t不代表end能到达t。即t为10,两头牛1 6,7 9.无法到达终点。
代码如下:
#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;struct node {int s, e;}a[25005];bool cmp(node x, node y) {if(x.s == y.s)return x.e < y.e;return x.s < y.s;}int main() {int n, t, cnt = 0, Max;scanf("%d%d", &n, &t);for(int i = 0; i < n; i++){scanf("%d%d", &a[i].s, &a[i].e);}sort(a, a + n, cmp);int start = 0, end = 0;while(end < t) {Max = 0;for(int i = start; i < n && a[i].s <= end + 1; i++, start++) {Max = max(Max, a[i].e);}if(Max == 0)break;cnt++;end = Max;}if(end < t)cout << "-1\n";elsecout << cnt << "\n";return 0;}
阅读全文
0 0
- poj 2376 Cleaning Shifts( 贪心 )
- POJ 2376 Cleaning Shifts 贪心
- poj 2376 Cleaning Shifts( 贪心 )
- poj 2376 Cleaning Shifts 贪心
- 贪心-poj 2376-Cleaning Shifts
- POJ 2376 - Cleaning Shifts(贪心)
- Cleaning Shifts - POJ 2376 贪心
- Cleaning Shifts (poj 2376 贪心)
- Cleaning Shifts - POJ 2376 贪心
- [贪心] poj 2376 Cleaning Shifts
- [贪心] poj 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts [贪心]
- Poj 2376 Cleaning Shifts【贪心】
- POJ 2376 Cleaning Shifts 贪心
- POJ 2376 Cleaning Shifts(贪心)
- POJ 2376 Cleaning Shifts 贪心
- POJ 2376 Cleaning Shifts(贪心)
- 【贪心+排序】poj 2376 Cleaning Shifts
- 快排的两种写法
- 解析java的hashmap死循环问题
- 多核任务处理
- Jupyter Notebook基本操作
- RocketMQ(一)——简介
- POJ 2376 Cleaning Shifts(贪心)
- 面试题
- Collection,Map
- 9.跨域
- java List接口实现总结
- 多线程
- Python中利用filter判断素数
- spring缓存(二)--redis缓存
- Hadoop-2.8.0 开发环境搭建(Mac)