Into a custom type in the set

The cause of this matter is in learning knapsack problem suddenly thought of an algorithm analysis of what should be n ^ 2logn complexity, of course, slower than the dp. But since the thought is realized under:

#include <bits / STDC ++ H.>
 a using  namespace std; 
typedef unsigned Long  Long LL;
 / * a very inefficient method, but it is their own mind, it easily achieves a bit. Essentially based optimization method is exhaustive, all combinations of items selected is not recycled, but circulation state. 
Look substantially, which must be faster than exhaustive because it incorporates a number of cases exhaustive, i.e., in the "same backpack now and the same total value of the items remaining space" in the case of different items to select. 
However, the complexity of which is at least n ^ 2 * logn level because it requires each article cycle set of elements and keep insert a new state, but also to the storage class, the speed should be slower * / 
class stateNow {
 public :
     int sizeRest;
     int valueAll;
     BOOL  operator <( const stateNow TE &) const {
         IF (( the this -> sizeRest <te.sizeRest) || (this->sizeRest==te.sizeRest&&this->valueAll>te.valueAll))
        return true;
        else 
        return false;
    }
};
int main(){
int n,size;
scanf("%d %d",&n,&size);
int *objectSize=new int[n];
int *objectValue=new int[n];
set<stateNow>::iterator it;
set<stateNow>stateCollection;
int maxValue=0;
 For ( int I = 0 ; I <n-; I ++ ) { 
    Scanf ( " % D% D " , & objectsize [I], & The objectValue [I]); // can not \ n is also applied to a string in, \ n end end flag is input, it reads the words will affect 
}
 for ( int I = 0 ; I <n-; I ++ ) {
         IF (! {stateCollection.empty ())
         for (stateCollection.begin iT = (); ! stateCollection.end IT = (); IT ++ ) { 
        stateNow TEMP; 
        temp.sizeRest = IT-> sizeRest- objectsize [I]; 
        temp.valueAll = The objectValue [I] + IT-> valueAll; 
        if(temp.sizeRest==0){
            if(temp.valueAll>maxValue)
            maxValue=temp.valueAll;
        }
        if(temp.sizeRest>0)
            stateCollection.insert(temp);
        }
        }
        stateNow temp;
        temp.sizeRest=size-objectSize[i];
        temp.valueAll=objectValue[i]; 
        stateCollection.insert(temp);
}
printf("%d",maxValue);
getchar();
}
}

Here involves passing a custom type of questions to set in, found a blog post, just talking about this issue, do not quote here the original text, click on the link and see: HTTPS: //www.cnblogs. com / shawnhue / archive / 2011/ 12/22 / set_comparison_strick_weak_ordering.html

Why do you need to define a custom comparison function object or <operator it? Because the inner set are ordered (red-black tree), and to ensure unique elements, it is necessary a method can be used to compare two elements. ( TWO Keys K1 and K2 are Considered equivalent to BE IF at The Comparison Object for CoMP, CoMP (K1, K2) == false && CoMP (K2, K1) == false. )

In addition, the blog post said, always makes the comparison function is relatively weak target to meet strict order, that is,

Strict weak ordering has the following attributes. For the set S of all x, y, z,

For all x, there is no x <x (non-reflexive --21 section heading that is the)

X is not equal for all y, if x <y then the absence of y <x (asymmetry)

For all x, y and z, if x <y, and y <z, then x <z (transitivity)

If x <y, then for all z, or x <z or z <y (or both established)

This is when Custom <operators to be noted that the operation is carried out otherwise undefined behavior, unexpected error.

Guess you like

Origin www.cnblogs.com/jiading/p/11108821.html