【贪心+排序】poj 2376 Cleaning Shifts
来源:互联网 发布:网络体育关注 编辑:程序博客网 时间:2024/03/28 18:30
先按照开始时间升序排序,再按照结束时间升序排序,每次都挑选结束时间最晚的区间,直到把整个区间覆盖。边界情况比较多,而且还有一个很违背常识的地方,上一头牛t时刻结束,下一头牛可以从t+1时刻开始,而不是t时刻。。。
#include <iostream>#include <algorithm>#include <utility>#include <stdio.h>using namespace std;int main(){int n, t;pair<int, int> inter[25005];scanf("%d%d", &n, &t);for(int i=0; i<n; i++)scanf("%d%d", &inter[i].first, &inter[i].second);sort(inter, inter+n);int res = 0;bool solution = true;if(inter[0].first != 1){solution = false;}int pre = 1;int cur = -1;for(int i=0; i<=n; i++){if(inter[i].first != 1 && inter[i-1].first == 1 && res==0){pre = inter[i-1].second;cur = -1;res++;if(pre >= t)break;i--;continue;}if(inter[i].first == 1)continue;if(inter[i].first<=pre+1 && i!=n){if(inter[i].second <= pre)continue;cur = max(cur, inter[i].second);}else{//cout<<"pre: "<<pre<<endl;if(i==n && cur<t){solution = false;break;}if(cur <= pre){solution = false;break;}pre = cur;cur = -1;res++;//cout<<inter[i].first<<" "<<inter[i].second<<endl; i--;if(pre >= t)break;}}if(solution == false)cout<<-1<<endl;elsecout<<res<<endl;//system("pause");return 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
- 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(贪心)
- 难忘
- 简单实现PHP分页
- USB HID report descriptor
- 动态变量和静态变量的区别,堆和栈的区别
- HDU/HDOJ 1372 Knight Moves(骑士游走问题) 简单广度优先搜索
- 【贪心+排序】poj 2376 Cleaning Shifts
- 题目1047:素数判定
- SICP 习题(1.1,1.2,1.3,1.4)解题总结,起步。
- python学习笔记
- 【算法和数据结构】_5_线性结构_单链表
- while ((read = getline(&line, &len, fp)) != -1)
- Memcached 一致性hash原理
- [Direct3D] Surfaces (Direct3D 9)
- VB与API学习笔记(9)程序间的会话