poj2376 Cleaning Shifts(区间贪心,理解题意)

https://vjudge.net/problem/POJ-2376

题意理解错了!!真是要仔细看题啊!!

看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<set>
 8 #define INF 0x3f3f3f3f
 9 typedef long long ll;
10 using namespace std;
11 typedef struct{
12     int x, y;
13 }Node;
14 Node node[100010];
15 bool cmp(const Node a, const Node b)
16 {
17     if(a.x != b.x)
18         return a.x<b.x;
19     else
20         return a.y>b.y;
21 }//起点相同则返回终点大的,否则返回起点小的
22 int main()
23 {
24     int n, t, cnt=1, flag=0;
25     cin >> n >> t;
26     for(int i = 0; i < n; i++){
27         cin >> node[i].x >> node[i].y;
28     }
29     sort(node, node+n, cmp);
30     if(node[0].x != 1){
31         cout << "-1" << endl;
32         return 0;
33     }
34     else if(node[0].y == t){
35         cout << "1" << endl;
36         return 0;
37     }
38     else {
39         int end = node[0].y, pre, maxm = node[0].y;
40         for(int i = 1; i < n; i++){
41             pre = maxm;
42             while(i < n&&node[i].x<=end+1){//这里理解错了!!
43                 if(maxm < node[i].y){
44                     maxm = node[i].y;
45                 }
46                 i++;
47             }
48             i--;
49             //cout << maxm << endl;
50             if(maxm == t){
51                 cnt++;
52                 flag=1;
53                 break;
54             }
55             else if(pre == maxm){
56                 break;
57             }
58             else{
59                 end = maxm;
60                 cnt++;
61             }
62         }
63         if(!flag) cout << "-1" << endl;
64         else
65             cout << cnt << endl;
66     }
67     return 0;
68 }

原文地址:https://www.cnblogs.com/Surprisezang/p/8997584.html

时间: 2024-10-13 00:49:59

poj2376 Cleaning Shifts(区间贪心,理解题意)的相关文章

poj2376 Cleaning Shifts 区间贪心

题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大区间的左端相差1.这个是看题解才知道的. 解题思路: 贪心题的关键是找到贪心策略.但是这题的贪心策略没那么明显.并且贪心策略没有特定地去选择某一区间.这一题最重要的是要知道在什么情况下才需要增加一个区间. 首先是进行排序,按照区间的左端从小到大排序,左端相同的按照右端从小到大排. 从头开始遍历(只能

POJ2376 Cleaning Shifts 【贪心】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11542   Accepted: 3004 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

poj 3171 Cleaning Shifts(区间的最小覆盖价值)

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2743   Accepted: 955 Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer J

poj2376 Cleaning Shifts【线段树】【DP】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

POJ2376 Cleaning Shifts(贪心)

给出每头奶牛的覆盖区间,求最少几头奶牛可以覆盖[1,T]这个区间.(有个问题需要注意一下,比如T=10,1-5,6-10这就算全部覆盖了,覆盖的是点) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define rep(i,n) for(int (i)=0;(i)<n;(i)++) using namespace std; struct Node {

POJ 2376 Cleaning Shifts (贪心)

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first bei

poj 2376 Cleaning Shifts(贪心)

Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), t

ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), t

POJ2376 Cleaning Shifts

http://poj.org/problem?id=2376 类似于工作排序问题 贪心策略:在符合时间情况的选项中 选择结束时间最迟的牛 具体步骤: 按照开始时间升序排列  如果 开始时间相同 按照结束时间升序排列 设t为最终结束时间 区间[1, t]为最终区间 一次1 to n的循环 同时 扩大区间 最后比较t和T的值即可 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 usi