Time limit on Test 2 Help. (C++)
Posted by
Ivailo 15 Aug 2013 18:12
Where do I have a problem?
I used binary search and got time limit on test 2. But when I use N*N1 speed I pass test 2 and get time limit on test 10.
Can you please check my code. I believe it is correct.
#include <iostream>
using namespace std;
bool binary_search (long* b , long a , long from ,long to)
{
while (from != to)
{
long mid = (from + to )/2;
if (b[mid] + a == 10000) return true;
if (b[mid] + a > 10000)
{
from = mid;
}
if (b[mid] + a < 10000)
{
to = mid;
}
}
return false;
}
int main ()
{
long n , n1;
long* a , *b;
cin >> n;
a = new long[n];
for (int i =0 ; i < n ; i++)
{
cin >> a[i];
}
cin >> n1;
b = new long[n1];
for (int i =0 ; i < n1 ; i++ )
{
cin >> b[i];
}
bool tr = false;
for (int i = 0 ; i < n ; i++)
{
tr = binary_search (b , a[i] , 0 , n1);
if (tr) break;
}
if(tr) cout << "YES";
else cout << "NO";
}
Re: Time limit on Test 2 Help. (C++)
Posted by
Ivailo 16 Aug 2013 12:36
Ok my bad. Why do I get TLE when I use binary, but don't get TLE when I use n1*n? Isn't binary faster?