HDU 1257nyoj814 + FIS interceptor missile rises longest sequence length

Served, has been wrong answer, had to get up, change has been found that there is only one place is not the same, is that the data is a group of a group, is to keep reading. I read only once. Such a point of error, get afternoon. . . .

The following is the code:

I myself obtained directly with the greedy, the comments section is another method, in fact, a closer look is the same.

Greedy idea: save a minimum height of each interception system now with vec. The first is initialized to the height of a missile, then for each missile, exists or not is determined lower_bound (> = the first number), if so, update the current value of the height, or, insertion push_back

 

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
	int n = 0;
	while (scanf("%d", &n) != EOF) {
		vector<int> vec;
		int k = 0;
		scanf("%d", &k);
		vec.push_back(k);
		//int cnt = 1;
		for (int i = 1; i < n; i++) {
			scanf("%d", &k);
			//printf("%d ", cnt);
			//求最长上升子序列的长度
			/*if (vec[vec.size() - 1] < k) {
				vec.push_back(k);
			}
			else {
				vector<int>::iterator iter = lower_bound(vec.begin(), vec.end(), k);
				*iter = k;
			}*/

			//求非上升子序列的个数
			sort(vec.begin(), vec.end());
			vector<int>::iterator iter= lower_bound(vec.begin(), vec.end(), k);
			if (iter != vec.end()) {
				*iter = k;
			}
			else {
				vec.push_back(k);
			}

		}
		printf("%d\n", vec.size());
	}

	//system("pause");
	return 0;
}

Another method is principle:

Longest seek increased sequence FIS:

      Given the number of sorted column pile, seeking LIS length thereof. Its length is that it is non-LIS increased number sequence.

Comments in this way is then time complexity is n2

Greedy method is simple and easy to understand, and not dynamic programming. Dynamic programming itself is easy to understand, but to be optimized to nlogn irritated.

For details, see the following two articles:

http://www.cnblogs.com/wxjor/p/5524447.html

http://www.cnblogs.com/frankchenfu/p/7107019.html

#include<cstdio>
#include<algorithm>
const int MAXN=200001;

int a[MAXN];
int d[MAXN];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    d[1]=a[1];
    int len=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]>d[len])
            d[++len]=a[i];
        else
        {
            int j=std::lower_bound(d+1,d+len+1,a[i])-d;
            d[j]=a[i]; 
        }
    }
    printf("%d\n",len);    
    return 0;
}

Note: In the dynamic programming method, the following requirements lower_bound else in fact did not need, that is, The second said with a stack mimics the longest rising sequence did not need to, because just the length of it, as long as the top element always up to date on it.

The first chapter necessity of this approach with "potential" explanation, I feel far-fetched. There is no need to do so that we can seek to get length. Note lower_bound is the complexity of logn. If you keep using the STL, binary search can be achieved. I.e. as the first to the end of the code

I just think the above is, I did not find someone else in a binary search to explain very clearly, I thought it was useless. If you remove the sentence, then the complexity of the algorithm is O (N), that is, as long as the current number than the big stack, then counted.

But it is clear that the time complexity of the algorithm and greedy I think does not match, and, the following circumstances:

If the input is: 2534, if only one comparison, the stack is larger than the top of the stack, counting the number, then obviously result in this case is 2, 3 not.

Once again, we see what happens: When the judgment 2, although it is smaller than the top of the stack, but use the binary search to find else in its place, then update the position, that position is obviously top of the stack. This element is updated to the top of the stack. Thus ensuring the correctness of the algorithm.

In other words: not only update the top element in the current element is larger than the top of the stack when the same is also in else may be updated. That is, if the element> (top of the stack + 1) elements, this element must be updated to the top of the stack.

For the current number <= +1 stack elements, this case also need to execute else, the only way we can guarantee element stack +1 is updated.

And so on, we need to ensure that elements of the entire stack is full of such updates

The following situation: 21067345. Stack changes are as follows

 

 

Better understand this algorithm

 

 

Published 32 original articles · won praise 5 · Views 4660

Guess you like

Origin blog.csdn.net/qq_38941327/article/details/89318556