488 - Triangle Wave

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

Moderator: Board moderators

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 488 TLE

Post by Obaida » Wed May 28, 2008 11:05 am

I am sending you a code by PM you better check it.
And trace the method.
try_try_try_try_&&&_try@try.com
This may be the address of success.

hahahaken
New poster
Posts: 26
Joined: Tue May 27, 2008 10:42 am

Re: 488 TLE

Post by hahahaken » Wed May 28, 2008 11:54 am

Obaida wrote:I am sending you a code by PM you better check it.
And trace the method.
Actually mine is similar to yours and I submit my code again, still time limit exceeded
Is it related to repeatedly calling the function in my program?

Code: Select all

Removed after AC
Last edited by hahahaken on Fri May 30, 2008 10:34 am, edited 1 time in total.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 488 TLE

Post by Obaida » Wed May 28, 2008 12:26 pm

Try it removing the function. Hope it will work.
try_try_try_try_&&&_try@try.com
This may be the address of success.

hahahaken
New poster
Posts: 26
Joined: Tue May 27, 2008 10:42 am

Re: 488 TLE

Post by hahahaken » Wed May 28, 2008 12:37 pm

Obaida wrote:Try it removing the function. Hope it will work.
I have already removed the function, TLE AGAIN, NO!!!
Pls take a look at my code, I think there should be something wrong.

Code: Select all

Removed after AC
Last edited by hahahaken on Fri May 30, 2008 10:35 am, edited 1 time in total.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 488 TLE

Post by Obaida » Wed May 28, 2008 12:44 pm

Those cin and cout are slow function.
Remove them and use printf and scanf.
try_try_try_try_&&&_try@try.com
This may be the address of success.

hahahaken
New poster
Posts: 26
Joined: Tue May 27, 2008 10:42 am

Re: 488 TLE

Post by hahahaken » Wed May 28, 2008 12:54 pm

Obaida wrote:Those cin and cout are slow function.
Remove them and use printf and scanf.
The version you sent me also uses cin and cout, it's a AC, right?
Mine is not just only exceeded for a few seconds, but at least 5 - 10s, coz I keep refreshing firefox and the status keep "running" until TLE appears.
Then, the problem is that why you use cin and cout and you got correct answer but mine got TLE?

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 488 TLE

Post by Obaida » Wed May 28, 2008 1:14 pm

You should check it out, and i think it's possible for you.
So why it's me? :o
try_try_try_try_&&&_try@try.com
This may be the address of success.

DJWS
Learning poster
Posts: 100
Joined: Sat Oct 11, 2003 3:30 pm
Location: Taiwan
Contact:

Re: 488 TLE

Post by DJWS » Thu May 29, 2008 7:42 pm

Obaida wrote:Those cin and cout are slow function.
Remove them and use printf and scanf.
In this problem, you can replace "endl" with "\n" and get reasonable runtime. "endl" push a character "\n" into I/O buffer and flush the buffer immediately. Frequent flushing will take a lot of time, especially with massive I/O data.

Although "endl" is somehow slower than single "\n", "endl" is not bad at all. The advantage of "endl" is that we can ensure the I/O data printing on the screen, line by line. Even though the program crashed, we still can see all messages generated by the program before the crashing. The advantage of "endl" is helpful when debugging.

BTW, cin and cout can be faster if we set up this:

Code: Select all

cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
Thus cin and cout are almost as fast as scanf and printf.

hahahaken
New poster
Posts: 26
Joined: Tue May 27, 2008 10:42 am

Re: 488 TLE

Post by hahahaken » Fri May 30, 2008 10:43 am

Finally got AC, thanks
But I want to know why the following can make my program run faster:
1. What's the difference between "endl" and "\n"?
2. What's the difference between "cin and cout" and "scanf and printf"?
3. Why "cin.sync_with_stdio(false);" and "cout.sync_with_stdio(false);" can make my program run faster?

DJWS
Learning poster
Posts: 100
Joined: Sat Oct 11, 2003 3:30 pm
Location: Taiwan
Contact:

Re: 488 TLE

Post by DJWS » Fri May 30, 2008 12:58 pm

hahahaken wrote:Finally got AC, thanks
But I want to know why the following can make my program run faster:
1. What's the difference between "endl" and "\n"?
2. What's the difference between "cin and cout" and "scanf and printf"?
3. Why "cin.sync_with_stdio(false);" and "cout.sync_with_stdio(false);" can make my program run faster?
They are good questions. But I cannot explain them within few words (because I am inarticulate in English). It needs deeper views about the hardware architecture and about C/C++ languages. You may google or ask others for the details. :)

User avatar
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Re: Crazzzzzzzzzzzzy WA...488

Post by newton » Mon Jun 09, 2008 10:38 pm

There would be only one test case.
You need not to handle it upto EOF.
just change
** You must avoid printing newline at the last of output.

Code: Select all

 while(scanf("%d",&n) ==1){
-------
-------
}
to

scanf("%d",&n);
------
------

mrmrumman
New poster
Posts: 10
Joined: Sat Jul 05, 2008 11:26 pm
Location: Dhaka
Contact:

Re: WA 488

Post by mrmrumman » Wed Jul 09, 2008 10:08 pm

Why I get wrong anser with my code what is the solution?

Code: Select all

[b]# include <stdio.h>
int main()
{
	int cases,i,height,frequency,j;
	char line;
	while(scanf("%d",&cases)==1)
        {
		for(i=0;i<cases;i++)
		{
			scanf("%d",&height);
			scanf("%d",&frequency);
			for(int l=0;l<frequency;l++)
			{
				if(l!=0)
					printf("\n\n");
				for(j=1;j<=height;j++)
				{
					for(int k=0;k<j;k++)
						printf("%d",j);
					printf("\n");
				}
				for(j=height-1;j>=1;j--)
				{
					if(j!=height-1)
						printf("\n");
					for(int k=0;k<j;k++)
						printf("%d",j);

				}

			}
		}
	
        }
	return 0;
}[/b]
R|_|\/|\/|/-\|\|

mrmrumman
New poster
Posts: 10
Joined: Sat Jul 05, 2008 11:26 pm
Location: Dhaka
Contact:

Re: WA 488

Post by mrmrumman » Thu Jul 10, 2008 10:39 am

any1 can help why I get WA?

Code: Select all

# include <stdio.h>
int main()
{
	int cases,i,height,frequency,j;
	
	scanf("%d",&cases);
	for(i=0;i<cases;i++)
	{
		scanf("%d",&height);
		scanf("%d",&frequency);
		for(int l=0;l<frequency;l++)
		{
			if(l!=0)
				printf("\n");
			printf("\n");
			for(j=1;j<=height;j++)
			{
				for(int k=0;k<j;k++)
					printf("%d",j);
				printf("\n");
			}
			for(j=height-1;j>=1;j--)
			{
				if(j!=height-1)
					printf("\n");
				for(int k=0;k<j;k++)
					printf("%d",j);
			}
		}
	}
 	return 0;
}
R|_|\/|\/|/-\|\|

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Re: WA 488

Post by emotional blind » Thu Jul 10, 2008 10:49 am

You should not print a blank line at the very beginning.

mrmrumman
New poster
Posts: 10
Joined: Sat Jul 05, 2008 11:26 pm
Location: Dhaka
Contact:

Why WA in 488

Post by mrmrumman » Thu Jul 10, 2008 10:52 am

What is the problem with code any problem with my code? Is the way I taking input is right? Please help...

Code: Select all

# include <stdio.h>
int main()
{
	int cases,i,height,frequency,j;
	
	scanf("%d",&cases);
	for(i=0;i<cases;i++)
	{
		scanf("%d",&height);
		scanf("%d",&frequency);
		for(int l=0;l<frequency;l++)
		{
			if(l!=0)
				printf("\n");
			
			for(j=1;j<=height;j++)
			{
				for(int k=0;k<j;k++)
					printf("%d",j);
				printf("\n");
			}
			for(j=height-1;j>=1;j--)
			{
				if(j!=height-1)
					printf("\n");
				for(int k=0;k<j;k++)
					printf("%d",j);
			}
			printf("\n");
		}
	}
 	return 0;
}
R|_|\/|\/|/-\|\|

Post Reply

Return to “Volume 4 (400-499)”