"Algorithm contest Step-up Guide" study concluded #include <algorithm>

  This afternoon at roughly finished school content Advanced Guide algorithm header files, in a summary here.

  reverse flip

  As the name suggests, is inverted for reverse operation of the original sequence, it is understood very simple, it is not repeated herein.

  Operation Example:

 

#include<bits/stdc++.h>
using namespace std;
vector<int>a;
int b[233];
int main() 
{
    int Na, nb; 
  achieve // vector of scanf(
"%d",&na); for(int i=0;i<na;i++) { int x; scanf("%d",&x); a.push_back(x); } reverse(a.begin(),a.end()); for(int i=0;i<na;i++) printf("%d ",a[i]); cout << endl;
  achieved under // array scanf(
"%d",&nb); for(int i=1;i<=nb;i++) scanf("%d",&b[i]); reverse(b+1,b+1+nb); for(int i=1;i<=nb;i++) printf("%d ",b[i]); return 0; }

 

unique deduplication


unique meaning still well understood ovo, I do not say too much, the function returns the value of the number of elements can be de-duplicated, such as:

int m=unique(a.begin(),a.end())-a.begin();

int n=unique(b+1,b+1+len)-b-1;

Operation Example:

#include<bits/stdc++.h>
using namespace std;
vector<int>a;
int b[233];
int na,nb;
int main( )
{
    scanf("%d",&na);
    for(int i=0;i<na;i++)
    {
        int x;
        scanf("%d",&x);
        a.push_back(x);
    }
    int ma=unique(a.begin(),a.end())-a.begin();
    for(int i=0;i<ma;i++)
    printf("%d ",a[i]);
    cout<<endl;
    scanf("%d",&nb);
    for(int i=1;i<=nb;i++)
    scanf("%d",&b[i]);
    int mb=unique(b+1,b+1+nb)-b-1;
    for(int i=1;i<=mb;i++)
    printf("%d ",b[i]);
    return 0;
}

random_shuffle random upset

Usage and reverse the same, I am too lazy to write code ...

 

sort quick sort

Presumably sort of general usage we are all familiar, and not repeat, but the vector <struct> I had no contact down.

I think there are many online blog introduced, but it seems all too clear, so he worked out a troublesome occupy space but easy to understand, and I hope dalao pointing.

Walkthrough:

#include<bits/stdc++.h> 
using namespace std;
struct node
{
    int x,y;
};
node b[233];
vector<node>a;
bool operator <(const node &a,const node &b)
{
    return a.x<b.x;
}
int main ()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
    scanf("%d%d",&b[i].x,&b[i].y);
    a.push_back(b[i]);        
    }
    sort(a.begin(),a.end());
    for(int i=0;i<a.size();i++)
    printf("%d %d\n",a[i].x,a[i].y);
    return 0;
}

permutation full array

Combinatorics we must more or less have to understand, the whole arrangement refers to all permutations Method A (n, n) type, which means that five selected five.

next_permutation () will get [first, last) of the marked sequence of a permutation and combination;

Next_permutation using the return value, it is determined whether the end of the whole arrangement, if there is no next permutations will return false;

Otherwise, return true; STL Providing two algorithms for calculating the permutation and combination relation; next_permutation and are prev_permutation;

The next full array (next_permutation) before a full array (prev_permutation)

simply put

next_permutation
lexicographic order of ascending full array

prev_permutation
according full array descending lexicographic order

Both the return value is true / false used to determine whether there is a full array of the normal output to the next for-loop arrangement

Note: both are inverses

#include<bits/stdc++.h>
using namespace std;
int a[25],b[25];
int main()
{
    int n1,n2;
    COUT << " operation on the next_permutation \ n- " ;
    cin>>n1;
    for(int i=1;i<=n1;i++)
    CIN >> A [I]; // input data should be a full set of data arrangement is not a lexicographical largest class 
    do
    {
        for(int i=1;i<=n1;i++)
        cout<<a[i]<<" ";
        cout<<endl;
    }while(next_permutation(a+1,a+1+n1));
    cout<<endl;
    COUT << " operation on the prev_permutation \ n- " ;
    cin>>n2;
    for(int i=1;i<=n2;i++)
    CIN >> B [I]; // input data should be a full set of data arrangement is not lexicographically smallest one class 
    do
    {
        for(int i=1;i<=n2;i++)
        cout<<b[i]<<" ";
        cout<<endl;
    }while(prev_permutation(b+1,b+1+n2));
    return 0;
}

lower_bound与upper_bound

 

l_b action is to find the position of the first element is greater than equal to x is within a range, is to find U_B
the first element is larger than the position x, the position of its return value.

Of course, there are other operations, such as it has a very important role is unique functions and supporting the use of discrete, specific follow-up I'll explain in a summary of the STL.

 

#include<bits/stdc++.h>
using namespace std;
vector<int>a;
int b[233];
int na,nb,xa,xb;
int main()
{
    scanf("%d%d",&na,&xa);
    for(int i=0;i<na;i++)
    {
        int x;
        scanf("%d",&x);
        a.push_back(x);
    }
    printf("%d\n",lower_bound(a.begin(),a.end(),xa)-a.begin());
    scanf("%d%d",&nb,&xb);
    for(int i=1;i<=nb;i++)
    {
        scanf("%d",&b[i]);
    }
    printf("%d\n",upper_bound(b+1,b+1+nb,xb)-b);
    return 0;
}

I summarize the main code-based, function algorithm at all easy to understand, do not use too much text, are their own hands to fight the test operation example, multi-test several sets of data, naturally understand.

If there is something wrong, correct me hope dalao. > W <

 

Guess you like

Origin www.cnblogs.com/valentino/p/11141123.html