Basic algorithm template | CSDN creation punch in

Posted by jaypotter on Sun, 13 Feb 2022 08:46:54 +0100

1. Fast power algorithm template

Find mk%p, time complexity O(logk).

int qmi(int m, int k, int p)
{
    int res = 1 % p, t = m;
    while (k)
    {
        if (k&1) res = res * t % p;
        t = t * t % p;
        k >>= 1;
    }
    return res;
}

 

2. Binary search algorithm template

There are two bisection templates, which are applicable to different situations.
Algorithm idea: assuming that the target value is in the closed interval [l, r], reduce the interval length by half every time. When l = r, we find the target value.

Version 1
When we divide the interval [l, r] into [l, mid] and [mid + 1, r], the update operation is r = mid or l = mid + 1;, There is no need to add 1 when calculating mid.

C + + code template:

int bsearch_1(int l, int r)
{
    while (l < r)
    {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    return l;
}

Version 2
When we divide the interval [l, r] into [l, mid - 1] and [mid, r], the update operation is r = mid - 1 or l = mid;, At this time, in order to prevent dead circulation, add 1 when calculating mid.

C + + code template:

int bsearch_2(int l, int r)
{
    while (l < r)
    {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    return l;
}

 

3. Quick sort template + understanding

Template code

void quick_sort(int q[], int L, int R){

    if(L >= R) return;

    int x = q[L], i = L - 1, j = R + 1;
    while(i < j){
        do i ++; while(q[i] < x);
        do j --; while(q[j] > x);
        if(i < j) swap(q[i], q[j]);
    }

    quick_sort(q, L, j);
    quick_sort(q, j + 1, R);
}

The relationship between the selection of x and i, j
If x = q[R], there will be problems using the above template,
For example, when l = R - 1 and R = R, execute quick_ When sort (Q, R - 1, R),
If R is not greater than [x - while], then it will jump out of the loop,
Next, execute quick_sort(q, L, j) is quick_sort(q, R - 1, R) will fall into infinite recursion,
So you can use quick at this time_ Sort (Q, l, I - 1) and quick_sort(q, i, R) to avoid this situation.
Similarly, quick cannot be used when x = q[L]_ Sort (Q, l, I - 1) and quick_sort(q, i, R).

 

4. Summary of double pointer template

Double pointer
The so-called double pointer algorithm means that in the process of traversal, instead of using a single pointer for circular access, it uses two pointers in the same direction or opposite direction for scanning, so as to achieve the corresponding purpose
.
In other words, the two fingered needle method makes full use of the feature of array order, which can simplify some operations in some cases and reduce the time complexity from log(n^2) to log(n)

(Note: the pointer here does not specifically refer to the concept of pointer in c, but refers to index, cursor or pointer, iteratable object, etc.)!

Array summation
If I give you two ordered arrays A1, A2, A3, an,b1,b2,b3...... bn; I want you to find two subscripts so that the sum of their corresponding values is equal to a specific value K and output a pair. Of course, we can easily think of using violent enumeration and double loop. Its time complexity is log (n^2). If the data exceeds 100000, the program times out, and we can use the double pointer algorithm to reduce it to log(n), which greatly saves time, In one second, we can process an array as large as 1000000. Let's take a lo ok at the code.

Title Link: https://www.acwing.com/problem/content/description/802/

Template:

for (int i = 0, j = 0; i < n; i ++ )
{
    while (j < i && check(i, j)) j ++ ;

    // Logic of specific problems
}

Code:

#include<iostream>
using namespace std;
const int N=1000010;
int a[N],b[N];
int n,m,k;

int main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=m;i++)cin>>b[i];

    for(int i=1,j=m;i<=n;i++){
       while(j>0&&a[i]+b[j]>k)
        {
             j--;
        }
        if(j>0&&a[i]+b[j]==k)
          cout<<i-1<<" "<<j-1<<endl;
    }
    return 0;
}

Topics: Algorithm