贪心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 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.
我写的,不对,超时

#include<iostream>
#include<algorithm>
using namespace std;
struct Cow{
    int begin;
    int end;
};
int n,t,MAX=0,MIN=1000001;
bool cmp(Cow a,Cow b){
    if(a.begin==b.begin)return a.end>b.end;
    else return a.begin<b.begin;
}
int Search(Cow *c){
    int i,num=0;
    int m=MAX;
    for(i=0;i<n;i++){
        if(c[i].begin<=m+1&&c[i].end>MAX){
        MAX=c[i].end;num=i;
        }
    }
    return num;
} 

int main(){
    while(cin>>n>>t){
        Cow *c=new Cow[n];
        for(int i=0;i<n;i++){
            cin>>c[i].begin>>c[i].end;
        }
        sort(c,c+n,cmp);
        int cnt=1;
        MAX=c[0].end;
        MIN=c[0].begin;
        while(1){
            int num=Search(c);//找到下一头牛的标号 
            if(num==0)break;//表示没找到更大范围的 
            cnt++;

        }
        if(MAX!=t||MIN!=1)cout<<-1<<endl;
        else cout<<cnt<<endl;
    }
    return 0;
}
/*
4 12
1 7
3 9
8 12
2 5

3 10
1 7
3 6
6 10

5 100
1 24
67 89
15 48
48 68
89 100

*/ 

正确版本

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 26000;
int n, t;
struct cow {
    int l, r;
}a[maxn];
bool cmp(cow a, cow b) {
    if (a.l != b.l) return a.l < b.l;
    else return a.r > b.r;
}
int main() {
    while (scanf("%d%d", &n, &t) != EOF) {
        bool fl, fr;
        fl = false;
        fr = false;
        for (int i = 0; i < n; i++) { 
            scanf("%d%d", &a[i].l, &a[i].r); 
            if (a[i].l == 1) fl = true;
            if (a[i].r == t) fr = true;
        }
        if (!fl || !fr) printf("-1\n");
        else {
            sort(a, a + n, cmp);
            int ans = 1;
            int r = a[0].r;
            int mmax = a[0].r;
            int j = 0;
            while (1) {
                while (j + 1 < n && a[j + 1].l <= r + 1) {
                    j++;
                    if (a[j].r > mmax) mmax = a[j].r;
                }
                if (mmax != r) {
                    ans++;
                    r = mmax;
                }
                else {
                    if (j == n - 1)break;
                    else {
                        ans = -1;
                        break;
                    }
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}
全部评论

相关推荐

白火同学:只是实习的话,你这份简历应该也差不多了。真要优化的话,因为面实习的话,没有开发经验,面试更重视技术栈水平。 1、重视JavaSE的基础吧,集合、泛型算是比较基础的基础,多线程、反射、JVM内存模型才是基础; 2、技术栈写到具体的点,比如Elasticsearch的使用写到某个点,限制面试官自由发挥,防止问了相关问题最后又答不上,如果真没把握建议不写,降低面试官的心理预期; 3、技术栈不要重复,比如技术栈第二条和第八条可以合并改为“熟悉Redis中间件,包括基本数据结构、缓存策略、持久化机制,了解缓存三剑客及其解决方案,并有相关项目经验。”; 4、项目指标量化,比如“达到xx秒的响应速度”(不过这个就有点偏校招社招的要求了,实习简历不写也无伤大雅)。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务