10170 - The Hotel with Infinite Rooms
Moderator: Board moderators
10170 - The Hotel with Infinite Rooms
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]
[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]
-
- New poster
- Posts: 17
- Joined: Fri May 24, 2002 4:24 am
- Location: Taiwan
- Contact:
10170
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]
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]
-
- New poster
- Posts: 10
- Joined: Thu Jun 05, 2003 5:23 pm
- Location: CS, AIUB, Dhaka, Bangladesh
- Contact:
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).
Hope it helps!
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)
Why WA??
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;
}
-
- New poster
- Posts: 4
- Joined: Fri Jul 18, 2008 9:44 pm
Re: 10170 - The Hotel with Infinite Rooms
#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 !!!!!!![:(](./images/smilies/icon_frown.gif)
#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 !!!!!!
![:(](./images/smilies/icon_frown.gif)
Re: 10170 - The Hotel with Infinite Rooms
Why my program passed time limit!
![:cry:](./images/smilies/icon_cry.gif)
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.
This may be the address of success.
-
- New poster
- Posts: 4
- Joined: Fri May 23, 2008 6:55 am
- Location: Dhaka
- Contact:
Re: 10170 - The Hotel with Infinite Rooms
Use long long int.
Sopno dhaka mon akash choar.............