Codeforces functionality may be limited from June 18, 19:00 (UTC) to June 19, 3:00 AM (UTC) due to technical maintenance. Polygon will work as usual. ×

Is the following code for the problem "Print LIS in O(nlogn)" correct ?

Revision en1, by MinusRatingPhobia, 2021-05-22 13:00:57
int lis(vector<int> &a) {
    int n = a.size();
    const int INF = 1e9 + 1;
    vector<int> d(n+1, INF);
    d[0] = -INF;

    for (int i = 0; i < n; i++) {
        int j = upper_bound(d.begin(), d.end(), a[i]) - d.begin();
        if (d[j-1] < a[i] && a[i] < d[j])
            d[j] = a[i];
    }

    int ans = 0;
    for (int i = 0; i <= n; i++) {
        if (d[i] < INF)
            ans = i;
    }

    for(int i = 1;i <= ans; i++){
        cout<<d[i]<<" ";
    }
    return ans;
}
Tags #algorithms, #binary-search, #doubt, #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English MinusRatingPhobia 2021-05-22 13:00:57 599 Please help me understand whether or not this code prints correct LIS or not. (published)