Re: 488 TLE
Posted: Wed May 28, 2008 11:05 am
I am sending you a code by PM you better check it.
And trace the method.
And trace the method.
Actually mine is similar to yours and I submit my code again, still time limit exceededObaida wrote:I am sending you a code by PM you better check it.
And trace the method.
Code: Select all
Removed after AC
I have already removed the function, TLE AGAIN, NO!!!Obaida wrote:Try it removing the function. Hope it will work.
Code: Select all
Removed after AC
The version you sent me also uses cin and cout, it's a AC, right?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.Obaida wrote:Those cin and cout are slow function.
Remove them and use printf and scanf.
Code: Select all
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
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.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?
Code: Select all
while(scanf("%d",&n) ==1){
-------
-------
}
to
scanf("%d",&n);
------
------
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]
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;
}
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;
}