zoukankan      html  css  js  c++  java
  • POJ 2376 Cleaning Shifts 贪心

      这题有坑点就是下个区间的起点只要<=上个区间的终点+1即可,对区间的起点按升序排序贪心找

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    struct node
    {
        int low,up;
    }qj[25005];
    bool cmp(const node&a,const node&b)
    {
        return a.low<b.low;
    }
    int main()
    {
        int n,len;
        int ans=0;
        scanf("%d %d",&n,&len);
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&(qj[i].low),&(qj[i].up));
        }
        sort(qj+1,qj+n+1,cmp);
        //for(int i=1;i<=n;i++)
            //cout<<qj[i].low<<"  "<<qj[i].up<<endl;
        int upp;
        int maxx;//记录最大终点的区间的下班
        if(qj[1].low>1)
        {
            cout<<-1<<endl;
            return 0;
        }
        ans=0;
        upp=0;
        for(int i=1;i<=n;)
        {
    
             if(qj[i].low<=upp+1)
            {
                maxx=-1;
                while(i<=n&&qj[i].low<=upp+1)
                {
                    maxx=max(maxx,qj[i].up);
                    ++i;
                }
                ++ans;
                upp=maxx;
                if(upp>=len) {printf("%d
    ",ans); return 0;}
            }
            else
                break;
        }
        cout<<-1<<endl;
    }
  • 相关阅读:
    Linux
    python中元类
    POJ 1182 食物链
    POJ 1733 Parity game
    top 介绍
    周记 2014.8.31
    windows10配置python
    oracle执行update时卡死问题的解决办法
    An Easy Introduction to CUDA C and C++
    super()
  • 原文地址:https://www.cnblogs.com/eason9906/p/11754976.html
Copyright © 2011-2022 走看看