码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 3389--Cleaning Shifts安排值班(贪心)

时间:2017-10-28 21:58:37      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:line   zoj   online   tar   cow   math   .com   奶牛   sort   

    日常刷水题。。。

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=3389 

Solution

    每一只奶牛的空余时间都有一个 start 和 end 。。

    刚开始时间 t = 0 ,每一次取 start 小于或等于 t + 1 的牛当中end最大的一只,并更新 t 即可。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int N,T;
struct cow{
    int l,r;
}c[30000];
bool cmp(cow p,cow q){
    if(p.l==q.l) return p.r<q.r;
    return p.l<q.l;
}
int main(){
    int ans=0;
    scanf("%d%d",&N,&T);
    for(int i=1;i<=N;i++)
        scanf("%d%d",&c[i].l,&c[i].r);
    sort(c+1,c+1+N,cmp);
    int t=0,k=1;
    while(t<T&&k<=N){
        int s=t;
        while(c[k].l<=(t+1)&&k<=N){
            if(c[k].r>s) s=c[k].r;
            k++;
        }
        if(t==s) break;
        t=s;
        ans++;
    }
    if(t<T) ans=-1;
    printf("%d\n",ans);
    return 0;
}

  

  

This passage is made by Iscream-2001.

 

BZOJ 3389--Cleaning Shifts安排值班(贪心)

标签:line   zoj   online   tar   cow   math   .com   奶牛   sort   

原文地址:http://www.cnblogs.com/Yuigahama/p/7748108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!