Lower Bound-STL

  • + 1 comment

    Solving the problem itself was easy, but testing result said:

    Time limit exceeded Your code did not execute within the time limits. Please optimize your code. For more information on execution time limits, refer to the environment page

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
        int n, num, numOfQ, q, targetIndex;
        cin >> n;
        vector<int> ints;
        for(int i = 0; i < n; i++){
            cin >> num;
            ints.push_back(num);
        }
        
        cin >> numOfQ;
        
        for(int i = 0; i < numOfQ; i++){
            cin >> q;
            for(int j = 0; j < n; j++){
                if(q == ints[j]){
                    targetIndex = j;
                    cout << "Yes " << targetIndex + 1 << endl;
                    break;
                }else if(ints[j] > q){
                    targetIndex = j;
                    cout << "No " << targetIndex + 1 << endl;
                    break;
                }
            }
        }
        return 0;
    }