7-10 train schedule

Thinking

This problem is very like the longest rising sequence, write this question why so I can not tell, we built the queue increasing subsequence, so that each end of the queue and the number of new minimum value comparison every take, if less than the minimum end of the queue it can be placed in the queue.

The reason to do so, is simply the absence of a small number of large block number, then we can output a queue in accordance with the requirements of the subject.

Code

#include <bits/stdc++.h>
using namespace std;
vector<int> dq;

int main()
{
    int n,num;
    scanf("%d",&n);
    for (int i=0;i<n;i++) {
        scanf("%d",&num);
        vector<int>::iterator it=lower_bound(dq.begin(),dq.end(),num);
        if (it==dq.end()) {
            dq.push_back(num);
        }
        else {
            int ind=it-dq.begin();
            dq[ind]=num;
        }
    }
    printf("%d\n",dq.size());
    return 0;
}

Guess you like

Origin www.cnblogs.com/xyqxyq/p/12328834.html