最长上升子列

最长上升子列

1. map

Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.

In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:

typedef pair<const Key, T> value_type;

Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

map containers are generally slower than unordered_map containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

The mapped values in a map can be accessed directly by their corresponding key using the bracket operator ((operator[]).

Maps are typically implemented as binary search trees.

operation

find: Get iterator to element (public member function )
end: Return iterator to end (public member function )

2. vector

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

operation

push_back: Add element at the end (public member function )

3. Solution

read in the favorite strips

and butild the map from the color to its index
(Actually there’s a problem that one color can appear at several different place of the favorite strips)

        cin >>num;
        for(i=0; i<num; i++){
            cin >> tmp;
            favindex[tmp] = i;//这样可以方便地找到某个颜色所有的序号 
        } 

read in the current exist strips

using a iterator to find the

        cin>>sizeExist;
        j=0;
        for(i=0; i<sizeExist; i++){
            cin >> tmp;
            map<int,int>::iterator it=favindex.find(tmp);
            if (it!=favindex.end()){
                realExist.push_back(it->second);
            }
        }

main function of Dynamic programming

        for(i = 0; i<sizeExist; i++){
            m = 0;

            for (j=0; j<i; j++){
                if(realExist[j]<=realExist[i]){//可以放在这个后面 
                    if(d[i]<d[j]+1){
                        d[i]=d[j]+1;
                    }
                }
            }
            m=d[i]>m?d[i]:m;
            m+=1;
        }
        cout << m;

Full code

#include <iostream>
#include <vector>
#include <map>

using namespace std;
vector<int> num2index[300];

int main()
{
    int i,j,num, tmp, m, sizeExist, currentIndex;
    int bNum[201], bIndex[201];
    int exist[10001];

    map<int, int> favindex;
    vector<int> realExist, d;

    while(scanf("%d",&num)!=EOF){//不知道这种表述是否可以

        cin >>num;
        for(i=0; i<num; i++){
            cin >> tmp;
            favindex[tmp] = i;//这样可以方便地找到某个颜色所有的序号 
        } 

        i = 0;

        cin>>sizeExist;
        j=0;
        for(i=0; i<sizeExist; i++){
            cin >> tmp;
            map<int,int>::iterator it=favindex.find(tmp);
            if (it!=favindex.end()){
                realExist.push_back(it->second);
            }
        }

        sizeExist = realExist.size();
        d.assign(sizeExist,0);

        //下面开始使用动态规划方法求解 
        for(i = 0; i<sizeExist; i++){
            m = 0;

            for (j=0; j<i; j++){
                if(realExist[j]<=realExist[i]){//可以放在这个后面 
                    if(d[i]<d[j]+1){
                        d[i]=d[j]+1;
                    }
                }
            }
            m=d[i]>m?d[i]:m;
            m+=1;
        }
        cout << m;

    }   
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yfren1123/article/details/80147080