POJ 2376 Cleaning Shifts (区间覆盖)
来源:互联网 发布:网络机顶盒H3芯片 编辑:程序博客网 时间:2024/06/05 02:23
给定n个区间,要求选出若干个来完整地覆盖0~T
输入使用区间最小的数目
贪心思想:每次选择可以覆盖到“开头”且右边界最靠右的点
编写方法:加上排序和边界修正可较小编写代码的难度
贪心思想的证明:
假设选取了区间a1, a2, a3, a4, a5,a6来完成覆盖任务
另有区间b可以覆盖到a3的“开头”且右边界比a3靠右,则可以将a3替换为b,不会增加区间数量。
#include <stdio.h>#include <algorithm>#define MAREA 25010using namespace std;struct aa{ int l; int r;};aa area[MAREA];bool cmp(const aa &a, const aa &b){ return a.l < b.l;}int main(){ int num_cow, T; int res = 0; scanf("%d %d", &num_cow, &T); for (int i = 0; i < num_cow; ++i) scanf("%d %d", &area[i].l, &area[i].r); sort(area, area+num_cow, cmp); int now = 0; int beg = 1; while(beg <= T && now < num_cow) { int temp = -1; while(now < num_cow && area[now].l <= beg) { if(temp < area[now].r) temp = area[now].r; now++; } if(temp == -1) break; else { beg = temp+1; res++; } } if(beg>T) printf("%d\n", res); else printf("-1\n");}
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(区间覆盖)
- POJ 2376 Cleaning Shifts(区间覆盖问题)
- Cleaning Shifts POJ - 2376(区间覆盖,排序+贪心)
- POJ 2376 Cleaning Shifts (贪心区间最少覆盖)
- [poj 2376] Cleaning Shifts [最小区间覆盖 贪心]
- POJ 2376 - Cleaning Shifts(最小区间覆盖)
- poj 3171Cleaning 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 区间贪心
- Week1-1Dynamic Connectivity
- Hibernate配置
- Volley框架全解析
- Tomcat7+Redis存储Session
- 2015.9.5
- POJ 2376 Cleaning Shifts (区间覆盖)
- XCode单元测试
- 娜样美的观察者模式
- 最小生成树的两种算法
- 1718:Rank
- PHP学习(二)--变量
- java.lang.IllegalStateException: you have not supplyed the global app context info from SDKInitializ
- spoj -705 New Distinct Substrings--后缀数组
- 鸟哥的linux私房菜中推荐的linux学习网站