ACM-10784 - Time limit exceeded.

59 views
Skip to first unread message

bahador saket

unread,
Jun 12, 2010, 2:15:54 PM6/12/10
to FIT Cafeteria
Dear All

I am going to solve this question : 10784 - Diagonal but I faced with
this message from UVa.

"
Your submission with number 8031141 for the problem 10784 - Diagonal
has failed with verdict Time limit exceeded.
Your program used more CPU time than what is allowed for this problem.
That means that your algorithm is not fast enough or that it entered
into an infinite loop."

It is my program:
Is it slowly?



#include<iostream>
using namespace std;
int main()
{
long long int a;
int j=1;
while(cin>>a)
{
int i=3;
long long int result=0;

if(a==0) break;
while(a>result)
{
result= (i*(i-3))/2;
i++;
}
cout<<"Case "<<j<<": "<<i-1<<endl;
j++;
}
}


Cheers,
Bahador Saket

Josiah

unread,
Jun 13, 2010, 4:06:58 PM6/13/10
to FIT Cafeteria
Did you mean while(a<result) instead of while(a>result)? I sense an
infinite loop there otherwise.

J

bahador saket

unread,
Jun 14, 2010, 7:02:09 AM6/14/10
to fit-ca...@googlegroups.com
Dear Josiah

I think it is correct because basic Formula: Number of diagonal in i-gon is (i * (i-3) ) / 2.
so the result 
is number of diagonals.
attend to this trace:                                    i is the sides / result is diagonals/ a is the input
 
  i   |  a  | result
------|-----|----------
  3  | 11 |  3                a>result
  4  | 11 |  2                a>result
  5  | 11 |  5                a>result
  6  | 11 |  9                a>result
  7  | 11 |  14              a<result  // and 7 is the correct answer


I will be happy if other can participate in this issue.


Regards,
Bahador





Josiah

unread,
Jun 14, 2010, 8:46:41 AM6/14/10
to FIT Cafeteria
Sorry, I was just guessing and didn't exactly look at the question.

The problem with your program is that it cannot handle large inputs
(e.g. it fails when the input is 10000000000).

Check the type for your variable "i". I think the problem lies
there. :)

J.

johnsee

unread,
Jun 15, 2010, 11:23:09 PM6/15/10
to FIT Cafeteria
Dear bahador,

I would encourage you to direct your ACM/UVA enquiries to the MMU ACM
Interest Group instead.
http://groups.google.com/group/mmu-acm

regards,
john
Reply all
Reply to author
Forward
0 new messages