zoukankan      html  css  js  c++  java
  • Week14 猫睡觉问题

    Week14 猫睡觉问题

     

     

     

    思路分析:

      关于这道题,真的是印象深刻,做这道题的时候真的心态爆炸,你以为你已经排掉坑了,结果又会发现还有更大的坑,当你苟延残喘排掉大坑,你以为你可以了,结果还有大坑等着你,实在是感人肺腑啊。当然这道题WA了很多。

      当然这道题的基本思路还是简单的,首先是数据处理,对于大模拟数据格式的处理是非常常见的,我们把读入的时间格式统统转化成分钟,内部用分钟比较。然后做好格式转化的接口函数,方便读入输出。

      第一步是读入数据,在读入的时候我们就要判断符不符合要求了,若醒的时间太长,则判定不符合题意。这是第一层判断。

      第二步我们要维护一个醒着时间的节点,这个节点的开始和结束都记录着猫猫已经醒着的时间,然后我们遍历它的活动时间表,满足睡觉条件就睡觉,我们尽可能让猫多睡一会儿,每次判断时间间隔能不能睡觉,若可以睡,则直接睡觉,并把睡觉时间记录到答案ans中,方便输出结果,并更新猫猫醒着的时间。

      我们是以一天为单位来做的,然后坑就来了,首先是过夜问题,及时间过了零点,这个也好解决,我们把过夜之后的时间加上一天就可以了。以为解决所有的坑了吗?还没有,考虑一下这种情况,就是猫猫在一天中24小时都醒着,而在题目的数据范围中这个是允许的,但是这是不能的情况,因为一天醒着,第二天就是48小时了,这个是不可以的,然后就是跨夜醒着的问题,因为前面我们的处理都是以一天为单位的,可能在跨夜的时候出现问题,比如,零点之前猫猫醒着,零点之后也醒着,两端时间单独都满足要求,但是加一起就不可以了。我们在后面加一个判断即可解决这个问题了。

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
     
    #define  maxn  20
    
    
     
    
     
    struct node
    {
        int s;
        int e;
    };
     
    vector<node>v;
    int day = 24*60;
    node actnod[maxn+5];
         
    //时间转换  输入 str  00:00-22:22 ,  &start , 返回end  转化成分钟 
    int timeconv(string str,int &start){
        if(str.length()!=11){
    //        cout<<"testerror"<<endl; 
        }
        for(int i=0;i<str.length();i++){
            str[i]=str[i]-'0';
        }
        
        int sum=0;
        int temp=(str[0])*10+str[1];
        sum=temp*60;
        temp=str[3]*10+str[4];
        sum+=temp;
        start=sum;
        sum=0;
        temp=str[6]*10+str[7];
        sum=temp*60;
        temp=str[9]*10+str[10];
        sum=sum+temp;
        return sum;
    }   
    
    int callength(int start,int end){
        if(end-start<0){
            return end+day-start+1;
        }else{
            return end-start+1;
        }
    } 
    
    
    bool cmp(const node &a, const node &b)
    {
        return a.s<b.s;
    }
     
    
     
    int main(){
        
        int a, b;
        int n;
        int i;
        bool flag;
        char str[20];
        node temp, x;
        while(~scanf("%d %d", &a, &b)){
            
            a *= 60;
            b *= 60;
            scanf("%d", &n);
            for (i=1; i<=n; i++)
            {
                scanf("%s", str);
                actnod[i].s = (str[0]-'0')*600+(str[1]-'0')*60+(str[3]-'0')*10+(str[4]-'0');
                actnod[i].e = (str[6]-'0')*600+(str[7]-'0')*60+(str[9]-'0')*10+(str[10]-'0');
                if (actnod[i].e<actnod[i].s) actnod[i].e += day;
            }
            sort(actnod+1, actnod+n+1, cmp);
            v.clear();
            flag = false;
            for (i=1; i<=n; i++)
            {
                if (actnod[i].e-actnod[i].s+1>b)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                temp.s = actnod[1].s;
                temp.e = actnod[1].e;
                for (i=1; i<n; i++)
                {
                    if (actnod[i+1].s-1-temp.e>=a)
                    {
                        x.s = temp.e+1;
                        x.e = actnod[i+1].s-1;
                        if (x.s!=x.e) v.push_back(x);
                        temp.s = actnod[i+1].s;
                        temp.e = actnod[i+1].e;
                    }
                    else
                    {
                        temp.e = actnod[i+1].e;
                        if (temp.e-temp.s+1>b)
                        {
                            flag = true;
                            break;
                        }
                    }
                }
            }
            if (!flag)
            {
                if (actnod[1].s+day-1-temp.e>=a)
                {
                    x.s = (temp.e+1)%day;
                    x.e = (actnod[1].s-1+day)%day;
                    if (x.s!=x.e) v.push_back(x);
                }
                else if (v.size()>0 && (v[1].s-1+day)%day-temp.s+1<=b)
                {
                }
                else flag = true;
            }
            if (flag || v.size()==0)
                printf("No
    ");
            else 
            {
                printf("Yes
    %d
    ", v.size());
                sort(v.begin(), v.end(), cmp);
                for (i=0; i<v.size(); i++){
                    int temp1=v[i].s;
                    int h1=temp1/60;
                    int f1=temp1%60;
                    temp1=v[i].e;
                    int h2=temp1/60;
                    int f2=temp1%60;
                    printf("%02d:%02d-%02d:%02d
    ",h1,f1,h2,f2);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.2.25
    Elementary Methods in Number Theory Exercise 1.2.14
    图解欧几里德算法
    图解欧几里德算法
    Elementary Methods in Number Theory Exercise 1.2.14
    Android中的长度单位详解(dp、sp、px、in、pt、mm)
    分享下多年积累的对JAVA程序员成长之路的总结
    android异常之都是deamon惹的祸The connection to adb is down, and a severe error has occured.
    TomatoCartv1.1.8.2部署时报错
    JavaScript浏览器对象之二Document对象
  • 原文地址:https://www.cnblogs.com/Xu-SDU/p/13082734.html
Copyright © 2011-2022 走看看