POJ--2376 Cleaning Shifts(贪心)

发布时间 2023-04-20 16:17:26作者: 57one

记录
15:57 2023-4-20

http://poj.org/problem?id=2376

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

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), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

  • Line 1: Two space-separated integers: N and T

  • Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

  • Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

贪心,先排序一遍,first优先。先判断第一个是不是从1开始的,不是直接输出-1;然后就选满足条件(条件是first小于或等于已确定最长的时间+1,加1的原因是这里是左闭右闭)中second最大的那个,不断贪心。每次找到解后,下一次判断就从这个解开始,不然会TLE
遇到的问题:

  1. poj vector<<>> 语法报错 g++版本问题。
  2. vector会导致TLE,使用数组不会。(vector就是慢
  3. 写比较函数的时候命名成了compare。。。直接冲突了
  4. 还是g++版本的问题。compare中参数为 T &lhs, T &rhs会编译错误,去掉&就可以了。
  5. 还是g++版本的问题。这个版本不支持增强for循环。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<utility>
#define MAX_N 25000
#define MAX_T 1000000
using namespace std;
typedef long long ll;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;

int N, T;
int end_time = 0;
int result = 0;
int j = 0;
typedef struct {
    int first;
    int second;
} P;
P v[MAX_N];
// vector<std::pair<int, int> > v;
// vector<P> v;

bool comp(P lhs, P rhs) {
    return (lhs.first < rhs.first) || (lhs.first == rhs.first && lhs.second < rhs.second);
}

void solve() {
    sort(v, v + N, comp);
    do {
        int max_end_time = end_time;
        if(v[0].first != 1) {
            result = -1;
            break;
        } else {
            // for(pair<int, int> &e : v) {
            //     if(e.first <= end_time + 1 && e.second > max_end_time) {
            //         max_end_time = e.second;
            //     }
            // }
            
            for(int i = j; i < N; i++) {
                if(v[i].first <= end_time + 1 && v[i].second > max_end_time) {
                    max_end_time = v[i].second;
                    j = i;
                }
            }

            if(max_end_time > end_time) {
                result += 1;
                end_time = max_end_time;
            } else {
                result = -1;
                break;
            }
            if(end_time == T) {
                break;
            }
        }
    } while (true);
    printf("%d\n", result);
}

int main() {
    scanf("%d %d", &N, &T);
    for(int i = 0; i < N; i++) {
        scanf("%d %d", &(v[i].first), &(v[i].second));
    }
    solve();
}