ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1021. Sacrament of the Sum

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 ძამაანთ [Tbilisi SU] 15 Aug 2013 19:56
"Where do I have a problem?"
You've got TLE. That is a problem
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?