10191 - Longest Nap

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sabbir_alam_ufo
New poster
Posts: 16
Joined: Fri Nov 15, 2013 9:33 pm

Re: 10191 - Longest Nap

Post by sabbir_alam_ufo » Wed Aug 31, 2016 9:46 pm

My code passed every test case in uDebug. But still getting WA. Please help...

Code: Select all

#include <bits/stdc++.h>
using namespace std;
#define MAX 2000000000

bool cmp(pair<int,int>a,pair<int,int>b)
{
    if(a.first>b.first)
        return false;
    else if(a.first==b.first)
    {
        if(a.second>b.second)
            return false;
    }
    return true;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    int nCase=0;
    while(cin>>n)
    {
        vector<pair<int,int> >v;
        v.clear();
        int prev=600;
        int max=18*60;
        int maximum=0;
        int start=600;
        for(int i=0;i<n;i++)
        {
            int csh,csm,ceh,cem;
            char ch1,ch2;
            cin>>csh>>ch1>>csm>>ceh>>ch2>>cem;
            if(cin.get()!='\n') 
            {
                getchar();
            string a;
            getline(cin,a);
            }
            int curr_start=csh*60;
            curr_start+=csm;

            int curr_end=(ceh*60)+cem;
            v.push_back(make_pair(curr_start,curr_end));
        }
        sort(v.begin(),v.end(),cmp);
        for(int i=0;i<v.size();i++)
        {
            pair<int,int>p=v[i];
            int curr_start=p.first>prev?p.first:prev;
            int curr_end=p.second>prev?p.second:prev;
            //cout<<curr_start<<" "<<curr_end<<endl;
            int diff=curr_start-prev;
            //cout<<curr_start<<" "<<prev<<endl;
            if(maximum<diff)
            {
                maximum=diff;
                start=prev;
            }
            prev=curr_end;
        }
        int diff=max-prev;
        if(maximum<diff)
        {
            maximum=diff;
            start=prev;
        }
        nCase++;
        int startM,startH;
        startH=start/60;
        startM=start%60;
        int ff=0;
        cout<<"Day #"<<nCase<<": the longest nap starts at ";
        cout<<startH<<":";
        if(startM<10)
            cout<<"0";
        cout<<startM<<" and will last for ";
        
        int maxHr = maximum/60;
        int maxMn=maximum%60;
        int flag=0;
        if(maxHr)
        {
            flag=1;
            cout<<maxHr<<" hours";
        }
        if(flag)
           cout<<" and ";
        cout<<maxMn<<" minutes."<<endl;
    
    }
    return 0;
}

Post Reply

Return to “Volume 101 (10100-10199)”