10170 - The Hotel with Infinite Rooms

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

Moderator: Board moderators

10170 - The Hotel with Infinite Rooms

Postby M.Z » Fri Jul 26, 2002 7:52 am

What is wrong with my program?
[cpp]
#include<iostream.h>
#include<math.h>
#include<stdio.h>

const double eps=1e-14;
double cal(double S,double D)
{
if(S>=D) return S;
double temp;
double k;
temp=floor(sqrt(2*D))+1;
for(k=temp;k>0;k--){
if(((2*S+k-1)*k<2*D)&&(2*D-(2*S+k)*(k+1)<eps)) return (S+k);
}
}

void main()
{
double S;
double D;
while(cin>>S>>D){
printf("%.0lf\n",cal(S,D));
}
}[/cpp]
M.Z
New poster
 
Posts: 3
Joined: Sat Jul 20, 2002 4:48 am

10170

Postby yatsen » Fri Sep 13, 2002 10:40 am

I have the fomula:
(S+n)(n-S+1)/2 >= D
And n is my answer. But I got WA. Can anyone help me?
yatsen
Learning poster
 
Posts: 67
Joined: Fri Nov 23, 2001 2:00 am
Location: taiwan

Postby Yarin » Sun Sep 15, 2002 4:06 pm

I'm not sure what's wrong with your program, it seems to work with all testdata I can throw at it. However, my solution it's much shorter, and it doesn't have a for-loop. I guess the error is some rounding problem somewhere.
Yarin
Problemsetter
 
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume

Postby yatsen » Tue Sep 17, 2002 3:48 am

Can you post your solution?
Thanks! :D
yatsen
Learning poster
 
Posts: 67
Joined: Fri Nov 23, 2001 2:00 am
Location: taiwan

Postby Shih-Chia Cheng » Tue Sep 17, 2002 4:07 am

Since you have found the equation and you can solve it using little mathematics. You need only one line to produce the correct answer, don't you?

yatsen wrote:Can you post your solution?
Thanks! :D
Shih-Chia Cheng
New poster
 
Posts: 17
Joined: Fri May 24, 2002 4:24 am
Location: Taiwan

Postby yatsen » Tue Sep 17, 2002 5:27 am

Thank you for your hint "mathemantics".
It remind me of the mathemantics I learned in solving equation in high school.
So I use the fomula x=(-b+sqrt(b^2-4ac))/2a
I don't need for-loop for this problem. :P

I have got AC. Thank you again.
yatsen
Learning poster
 
Posts: 67
Joined: Fri Nov 23, 2001 2:00 am
Location: taiwan

10170

Postby ggll » Tue Jun 10, 2003 5:35 am

About problem #10170, I tried both binary search and direct attack as shown below. However, the binary search method got WA, while direct attack passed.

I generated > 20 million random test cases(using the limitation in the problem) and always got the same result.

Any idea what might be wrong? Anybody know the test data?

[java]
// binary search
long low = 1;
long hi = 1000000000;

while (low < hi) {
long mid = (low + hi) / 2;
long use = (S + (S + mid - 1)) * mid / 2;
if (use < D) {
low = mid + 1;
}
else
hi = mid;
}

long resA = low + S - 1;

// direct attack
double x = (Math.sqrt((2 * S - 1) * (2 * S - 1) + 8 * D) - 2 * S + 1) / 2.0;

long resB = (long)Math.ceil(x - 1e-10) + S - 1;
[/java]
ggll
New poster
 
Posts: 13
Joined: Tue Jun 10, 2003 5:15 am

A Bug

Postby cauchy » Sun Oct 12, 2003 6:07 pm

I had also problems with this problem.
In fact, there is a bug in the problem specification (or in tests). There is a test case where d == 0. Try assert (d>=1). For this case the answer should be s-1.
cauchy
New poster
 
Posts: 9
Joined: Thu May 29, 2003 11:37 pm
Location: Poznan

Postby Tanzim-Saqib » Sun Oct 26, 2003 10:48 pm

I am a very weak analytical thinker, so that I have used neither binary search nor derived any formula. I have just used a staright forward method.. (complete brute force).

Code: Select all
i=s+1, total=s

begin_of_the_loop
   if not (total<d) break
   total = total + i;
   i = i + 1
end_of_loop
print(i-1)

Hope it helps!
[Tanzim Saqib]
-----------------------------
Problemsetter of 10490
Website: http://tanzim.tk
Tanzim-Saqib
New poster
 
Posts: 10
Joined: Thu Jun 05, 2003 5:23 pm
Location: CS, AIUB, Dhaka, Bangladesh

Why WA??

Postby ranban282 » Tue Mar 06, 2007 6:17 pm

Does anyone have any idea or test data as to why this gives WA??

Code: Select all
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<cctype>
#include<stack>
#include<map>
#include<set>
#include<assert.h>
#define eps 1e-8
using namespace std;

int main()
{
   int a,b;
   while(cin>>a>>b)
   {
      //assert(b>=1);
      //if(b==0)
      //{printf("%d\n",a-1);continue;}
      int residue=(a*(a-1))/2;
      int n=b+residue;
      double ans=(sqrt((double)(1+8*n))-1.0)/2.0;
      printf("%.0lf\n",ceil(ans));

   }
   return 0;
}
ranban282
New poster
 
Posts: 37
Joined: Tue May 02, 2006 1:01 pm

Postby ranban282 » Tue Mar 06, 2007 6:19 pm

Also, I had tried assert(b>=1).
It gave RE.
It is completely unfair to have a test case which is contrary to problem statement.
ranban282
New poster
 
Posts: 37
Joined: Tue May 02, 2006 1:01 pm

Postby mf » Tue Mar 06, 2007 6:38 pm

The test cases are fine. Your program suffers from overflows.
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

Re: 10170 - The Hotel with Infinite Rooms

Postby iit2006015 » Fri Jul 18, 2008 9:47 pm

#include<iostream>
#include<cmath>
using namespace std;
int main()
{ double s,d;
while(cin>>s>>d)
{ double t=pow((double)s,(double)2);
double q=(double)2*(double)d;
double re=t+q-s;
re=((double)4*(double)re)+(double)1;
double pe=sqrt(re);
pe=(double)pe-(double)1;
pe=(double)pe/(double)2;
//cout<<pe<<" ";
cout<<round(pe)<<" ";
}
system("pause");
}
plz help why its giving wa even after rounding off !!!!!! :(
iit2006015
New poster
 
Posts: 4
Joined: Fri Jul 18, 2008 9:44 pm

Re: 10170 - The Hotel with Infinite Rooms

Postby Obaida » Sun Jul 27, 2008 9:28 am

Why my program passed time limit! :cry:
Code: Select all
deleted after accepted
Last edited by Obaida on Tue Aug 05, 2008 6:23 am, edited 1 time in total.
try_try_try_try_&&&_try@try.com
This may be the address of success.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10170 - The Hotel with Infinite Rooms

Postby Nishith csedu 1448 » Fri Aug 01, 2008 9:33 pm

Use long long int.
Sopno dhaka mon akash choar.............
Nishith csedu 1448
New poster
 
Posts: 4
Joined: Fri May 23, 2008 6:55 am
Location: Dhaka

Next

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 1 guest