POJ 2376 - Cleaning Shifts(贪心)
来源:互联网 发布:微商引流软件 编辑:程序博客网 时间:2024/04/26 13:03
题目:http://poj.org/problem?id=2376
题意:每一只牛工作一定的时间段,给一段时间,要求用最少的牛能够覆盖这一整段的时间。
思路:贪心。将时间段按照开始时间由小到大,开始时间相同结束时间由小到大排列。在符合条件的情况下,选择结束时间最大。
15.2.10 再写一遍。wa了很多次。因为没有意识到前一秒结束的,可以后一秒开始。
CODE:
#include<iostream>#include<stdio.h>#include<algorithm>const int M=25005;using namespace std;struct stu{ int st,ed;}S[M];bool cmp(stu a,stu b){ if(a.st==b.st) return a.ed<b.ed; return a.st<b.st;}int main(){ //freopen("in.in","r",stdin); int N,T; while(~scanf("%d%d",&N,&T)) { for(int i=0;i<N;i++) { scanf("%d%d",&S[i].st,&S[i].ed); } sort(S,S+N,cmp); if(S[0].st>1) { printf("-1\n"); continue; } int ans=1,i=0,ii=0; int ok=0; while(i<N)//在符合条件的情况下选择结束时间长的。使用了三个变量控制! { int t=0; for(int j=i+1;j<N;j++) { if(S[j].st>S[ii].ed+1) break; if(S[j].ed>=S[t].ed&&S[j].ed>=S[ii].ed+1) t=j; } if(t==0) { i++; } else { ii=t; i=ii; ans++; } } if(S[ii].ed==T) printf("%d\n",ans); else printf("-1\n"); } return 0;}
15.2.10 代码明显比半年前好看多了~~还是有进步的0.0就这么自我欺骗吧。。。
CODE:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;struct node { int st, ed; bool operator < (const node & x)const { if(x.st == st) return x.ed < ed; return x.st > st; }}cow[25005];int main(){//freopen("in", "r", stdin); int n, len; while(~scanf("%d %d", &n, &len)) { int maxn = 0; for(int i = 0; i < n; ++i) { scanf("%d %d", &cow[i].st, &cow[i].ed); } sort(cow, cow + n); if(cow[0].st > 1) { printf("-1\n"); continue; } int tmp = cow[0].ed, i = 1, ans = 1, led = cow[0].ed; if(tmp >= len) { printf("1\n"); continue; } while(i < n) { int ok = 0; while(tmp+1 >= cow[i].st && i < n) { led = max(led, cow[i].ed); ok = 1; i++; } if(!ok) break; if(led > tmp) ans++; tmp = led; if(tmp >= len) break; } if(tmp < len) ans = -1; printf("%d\n", ans); } 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
- JavaScript基础
- 算法题001--Evaluate Reverse Polish Notation
- 数组和指针——都是“退化”惹的祸
- JQuery监听鼠标点击哪一个按键.
- HDU1159 最长公共子序列
- POJ 2376 - Cleaning Shifts(贪心)
- bypy用法记录
- Maven项目如何引用jar包
- LeetCode -- UniqueBinarySearchTrees
- EC2 安装 Apache
- 解释程序
- jQuery练习--隐藏和显示效果
- c++ explicit初步了解
- java 参数传递方式