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

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Re: Compile Error 10191

Post by misof » Mon Mar 21, 2005 4:21 pm

Ghust_omega wrote:I have Fedora 3 and g++ 3.4.2 I wish to know What are the flags for Compile under Linux
Sadly, as far as I know the judge still uses something like gcc 2.95.4, so it may happen that that the judge can't compile some code your compiler can. A notorious example is that the compiler at UVa doesn't support typeof().

The usual problem is that common (i.e. also your) g++ includes most of the STL libraries automatically, so if you forget an explicit #include, the code will still compile on your machine. The compiler the judge uses doesn't do this, thus the result is a compile error.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Mon Mar 21, 2005 4:53 pm

misof wrote: Sumankar: I really don't understand what you are talking about. Both your points are wrong.
I had trouble, including both, though i am not sure if it was on linux or not, which is why i said maybe.
Moreover, he IS using the namespace std!
All he is doing is pulluting the whole namespace.When all that you need are
two `std:: ' before vector and sort, why not type in the extra chars rather than pollute the whole namespace?

Suman.

P.S: C is not C++, they are different languages and just because we can shoot ourselves in the foot does not mean we should.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Mon Mar 21, 2005 11:05 pm

misof wrote:Sadly, as far as I know the judge still uses something like gcc 2.95.4, so it may happen that that the judge can't compile some code your compiler can. A notorious example is that the compiler at UVa doesn't support typeof().
It does, but it's called __typeof() and as __typeof it also works in newer versions of g++.

User avatar
Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Post by Ghust_omega » Tue Mar 22, 2005 12:29 am

Thanks to sumankar,misof and Krzysztof Duleba for their quick answers,and advice I solve the problem and Get WA :( but after somes tries AC :) The problems was #include <algorithm>
Thanks In Adavance
TO sumankar,misof and Krzysztof Duleba KEEP POSTING !!

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Tue Mar 22, 2005 2:39 am

sumankar wrote:
misof wrote: Moreover, he IS using the namespace std!
All he is doing is pulluting the whole namespace.When all that you need are
two `std:: ' before vector and sort, why not type in the extra chars rather than pollute the whole namespace?

Suman.
Okay, I understand your point now. The source of the misunderstanding between us is just that our philosophy on this point differs.

When writing a larger piece of code for a SW company your approach has its advantages, no doubt about that. But for a programming contest all the programs we write are "quick&dirty". Here I see no problems with simply using the whole namespace -- it makes my code a little bit shorter, easier to read and write, and it causes no problems. I'm still saying that (in a programming contest!) by including "using namespace std;" in your program you lose nothing. I'll keep on doing it.

If you have a "counter-example" or some other thoughts on this topic, I'll be more than glad to read them.

[Well, the only "problem" I can remember was when (on TopCoder) I had to write a method called sort()... it took me a while to realize that if I want to use the sort() from STL I have to refer to it as to std::sort() ... but in fact this is NOT a problem with the issue we were discussing :) ]

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Tue Mar 22, 2005 6:42 am

misof wrote: Okay, I understand your point now. The source of the misunderstanding between us is just that our philosophy on this point differs.
I am relieved :) .
When writing a larger piece of code for a SW company your approach has its advantages, no doubt about that. But for a programming contest all the programs we write are "quick&dirty". Here I see no problems with simply using the whole namespace -- it makes my code a little bit shorter, easier to read and write, and it causes no problems. I'm still saying that (in a programming contest!) by including "using namespace std;" in your program you lose nothing.
My point, exactly.And I have a little more to add, when I was going home
yesterday, a little disturbed at having started what seemed like a holy flame war, I was thinking about quick & dirty hacks vs robust coding. I was unsure how important it was to code quick vs code correctly. And it took some time before I realised that even if it takes a little time, we should indulge in a little more robust coding - that helps later, if you start
running even before you can walk - you'll trip for sure.
I'll keep on doing it
The call is yours!
If you have a "counter-example" or some other thoughts on this topic, I'll be more than glad to read them.
None at this point of time :wink:

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Tue Mar 22, 2005 6:55 am

One more thing, since programmers are _lazy_ :D, if std:: hurts try this

Code: Select all

 typedef std::map<int, int> myMap;
That takes care of the `i can type five extra chars when coding' disease :D
Regards,
Suman.

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Location: bangladesh
Contact:

10191-why all the time WA

Post by 59557RC » Tue Aug 16, 2005 9:27 pm

i think my code outputs correctly . but why WA? any1 pls help me?

#include<stdio.h>
#include<string.h>


int main()
{
int i,j,k,n,m=0,t1[102],t2[102], max;
char time1[102][6],time2[102][6],app[256];
while(scanf("%d",&n)!=EOF){ max=0;
for(i=1;i<=n;i++) {scanf("%s %s",time1,time2);gets(app); }
strcpy(time2[0],"10:00");
strcpy(time1[n+1],"18:00");
for(i=0;i<=n+1;i++){
t1=((time1[0]-'0')*10+(time1[1]-'0'))*60+((time1[3]-'0')*10+(time1[4]-'0'));
t2=((time2[0]-'0')*10+(time2[1]-'0'))*60+((time2[i][3]-'0')*10+(time2[i][4]-'0'));

}

for(i=1;i<=n+1;i++){
k=t1[i]-t2[i-1];
if(k>max) {max=k;j=i-1;}
}
if(max/60==0) printf("Day #%d: the longest nap starts at %s and will last for %d minutes.\n",++m,time2[j],max%60);


else printf("Day #%d: the longest nap starts at %s and will last for %d hours and %d minutes.\n",++m,time2[j],max/60,max%60);







}




return 0;
}
aaa

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

10191

Post by Darko » Fri Nov 11, 2005 10:37 am

Description of the problem says "You can assume that no line will be longer than 255 characters", so I assumed that reading 255 characters would do. Actually, I never assumed anything, I just never changed the OJ's Java I/O code, saw '255' thought "well, that should do"... but it didn't.

Is newline character a character?

Darko

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

Re: [10191] - beware: input line length is 255 + '\n'

Post by tan_Yui » Sat Nov 12, 2005 4:37 pm

The newline character is not handled as a character in this problem
because if the newline character was also a character, we couldn't find where is the end of line.
You should read each line until it reaches the newline character,
and not count as the number of elements.

I used the function 'gets' for reading lines on C language.
To store the characters, I prepared the size of array 256.
The last elements of array is for NULL character.

Best regards.

Shuvra(CSE-BUET)
New poster
Posts: 19
Joined: Wed Jan 11, 2006 9:57 am
Location: Dhaka

10191(longest nap) wa for >infinity times

Post by Shuvra(CSE-BUET) » Fri Jan 13, 2006 8:20 am

I have checked many inputs.Now I find no way other than posting my code. Please tell me about the error .


(After Solaris' reply I got the case I ignored) So I made a change .Now I have ac.
Thanks to Solaris.

.................................................................................................
Code removed after ac
..........................................................................................................
Last edited by Shuvra(CSE-BUET) on Sat Jan 14, 2006 1:31 pm, edited 1 time in total.
Life is a challenge.

Solaris
Learning poster
Posts: 99
Joined: Sun Apr 06, 2003 5:53 am
Location: Dhaka, Bangladesh
Contact:

Post by Solaris » Fri Jan 13, 2006 11:10 pm

Check the followin input,
1
16:00 18:00 Lectures
Where's the "Any" key?

justjoy
New poster
Posts: 2
Joined: Mon Aug 28, 2006 9:05 am
Location: South Korea
Contact:

...Well

Post by justjoy » Mon Aug 28, 2006 1:43 pm

Output...
If minute of the longest nap start time is 0
You must print 00
No 0


p.s

I'm Korean Middle School Student..
I can't English very well.
sorry,

zaman
New poster
Posts: 8
Joined: Sun Jan 07, 2007 5:40 am
Location: dhaka

10191

Post by zaman » Sun Jan 07, 2007 10:15 pm

I've tested the inputs in the forum. But still getting wrong answer.Please suggest any sample input that doesn't pass it .Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void INSERT_SORT(int *l,int lim){
int j,i,key;
for(j=1;j<lim;j++){
key=l[j];
i=j-1;
while(i>-1&&l>key){
l[i+1]=l;
i=i-1;
l[i+1]=key;
}
}
}

void main(){

int s, i, temp, nap_duration, nap_start[102], nap_finish[102], nap_start1, nap_finish1,day = 1; char ch[300],*token;
bool flag = false;
//freopen("G:\\input.txt","r",stdin);

while( scanf("%d", &s) != EOF){

if(flag == true )
printf("\n");
flag =true;
//strcpy(starting_time, "10:00");
nap_start[0] = 600;
nap_duration = 0;
gets(ch);

for( i = 1; i <= s ; i++ ){
gets(ch);

token = strtok( ch," ");
nap_finish = ((( token[0] - '0' ) *10 + ( token[1] - '0' )) *60 ) + (( token[3] - '0' ) *10 + ( token[4] - '0' ));


token = strtok( NULL," ");
nap_start = ((( token[0] - '0' ) *10 + ( token[1] - '0' )) *60 ) + (( token[3] - '0' ) *10 + ( token[4] - '0' ));

}

INSERT_SORT(nap_finish,i);
INSERT_SORT(nap_start,i);
nap_finish = 1080 ;
for( int i = 1; i <= s ; i++ ){
temp = nap_finish - nap_start[i-1];

if( nap_duration < temp ){
nap_duration = temp;
nap_start1 = nap_start[i-1];
nap_finish1 = nap_finish;
}

}

temp = (nap_finish - nap_start[i-1]);

if( nap_duration < temp ){
nap_duration = temp;
nap_start1 = nap_start[i-1];
nap_finish1 = nap_finish;
}

printf("Day #%d: the longest nap starts at %d:",day,(nap_start1/60));

if((nap_start1%60) < 10)
printf("0%d and will last for ",(nap_start1%60));

else
printf("%d and will last for ",(nap_start1%60));

if( nap_duration < 60)
printf("%d minutes.",nap_duration);

else
printf("%d hours and %d minutes.",(nap_duration/60),(nap_duration%60));

day++;
}



}

thanks in advance.
Keep in posting...

mhayter1
New poster
Posts: 15
Joined: Wed Jul 26, 2006 10:00 am

What's the Longest nap?

Post by mhayter1 » Mon Apr 02, 2007 10:35 pm

Can someone please help me. This problem is supposed to be easy, but I'm having a very tough time with it. My output seem right. Can someone point out the error in my code or give my inputs to break it. Thank you!

Code: Select all

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;

struct events
{
    int s,e;
};

bool gre(const events &a,const events &b)
{
    if(a.s<b.s)return 1;
    return 0;
}
void myitoa(unsigned long long Val,string &STr1,int BAse)
{
    STr1="";
    static const char AsCII[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char tptr[70];
    int InDeX=0;
    for(unsigned long long i=Val;i!=0;i/=BAse)
    {
        tptr[InDeX]=AsCII[(i%BAse)];
        InDeX++;
    }
    bool WoRKed=0;
    for(int j=0;j<InDeX;j++)
    {
        STr1=tptr[j]+STr1;
        WoRKed=1;
    }
    if(!WoRKed)
        STr1="0";
}

int main()
{
    int num_events;
    string input;
    for(int day=1;cin>>num_events;day++)
    {
        cin.get();
        if(num_events==0)
        {
            cout<<"Day #"<<day<<": the longest nap starts at 10:00 and will last for 8 hours and 0 minutes.\n";
            continue;
        }
        int longesnap=0;
        vector<events>orderofact;
        for(int i=0;i<num_events;i++)
        {
            getline(cin,input);
            int hour,minutes;
            char buffer;
            istringstream iss(input);
            iss>>hour>>buffer>>minutes;
            int start=hour*60+minutes;
            iss>>hour>>buffer>>minutes;
            int end=hour*60+minutes;
            events t;
            t.s=start;
            t.e=end;
            orderofact.push_back(t);
        }
        sort(orderofact.begin(),orderofact.end(),gre);
        int realstart=600;
        int realend=1080;
        int maxdiff=0;
        int ls=600;
        if(maxdiff<orderofact[0].s-realstart)
        {
            maxdiff=orderofact[0].s-realstart;
            ls=realstart;
            //cout<<"first worked\n";
        }
        if(maxdiff<realend-orderofact[num_events-1].e)
        {
            maxdiff=realend-orderofact[num_events-1].e;
            ls=orderofact[num_events-1].e;
            //cout<<"second worked\n";
        }
        for(int i=1;i<num_events;i++)
        {
            if(maxdiff<orderofact[i].s-orderofact[i-1].e)
            {
                maxdiff=orderofact[i].s-orderofact[i-1].e;
                ls=orderofact[i-1].e;
                //cout<<"maxdiff= "<<maxdiff<<" ls= "<<ls<<endl;
            }
        }
        int shours=ls/60;
        int smin=ls%60;
        string starttime;
        myitoa(shours,starttime,10);
        string temp;
        myitoa(smin,temp,10);
        if(smin==0)
            temp+="0";
        starttime=starttime+":"+temp;
            
        if(maxdiff<60)
        {
            cout<<"Day #"<<day<<": the longest nap starts at "<<starttime<<" and will last for "<<maxdiff<<" minutes.\n";
        }
        else
        {
            int mhours=maxdiff/60;
            int mmins=maxdiff%60;
            string hours;
            myitoa(mhours,hours,10);
            string mins;
            myitoa(mmins,mins,10);
            cout<<"Day #"<<day<<": the longest nap starts at "<<starttime<<" and will last for "<<hours<<" hours and "<<mins <<" minutes.\n";
        }
    }
    return 0;
}
         
Thanks for you're help.

Post Reply

Return to “Volume 101 (10100-10199)”

Who is online

Users browsing this forum: No registered users and 1 guest