PAT Class A Prep - High Accuracy and Sorting

High precision

polynomial

  • How to use an array to represent a polynomial

take the index of
the array as the exponent take the contents of the array as the coefficient of the polynomial

  • Polynomial Multiplication
double a[N],b[N],c[2*N];
for(int i=0;i<N;i++){
    
    
        for(int j=0;j<N;j++){
    
    
            c[i+j]+=a[i]*b[j];
        }
    }

Store vector a in reverse order in vector b

vector b(a.rbegin(), a.rend());

High-precision addition template (vector)

vector<int> add(vector<int> u,vector<int> v){
    
    
    vector<int> uu(u.rbegin(),u.rend());
    vector<int> vv(v.rbegin(),v.rend());
    vector<int> c;
    for(int i=0,t=0;i<uu.size()||i<vv.size()||t;i++){
    
    
        int s = t;
        if(i<uu.size()) s+=uu[i];
        if(i<vv.size()) s+=vv[i];
        c.push_back(s%10);
        t=s/10;
    }
    reverse(c.begin(),c.end());
    return c;
}

Base conversion (convert r base to decimal) (Qin Jiushao algorithm)

int get(char c)
{
    
    
    if (c <= '9') return c - '0';
    return c - 'a' + 10;
}


LL calc(string n, LL r)
{
    
    
    LL res = 0;
    for (auto c : n)
    {
    
    
        if ((double)res * r + get(c) > 1e16) return 1e18;
        res = res * r + get(c);
    }
    return res;
}

Keep entering n until n is negative

 while(cin>>n,n>0){
    
    
        
    }

Qin Jiushao's algorithm z = z*d+yd is the base that needs to be converted to decimal, y is each item under this base

  • For the read data, if you need to perform different operations according to the data type, for example, if the number is read, add 1, and if it is a letter, return ascii, then we can first store the data in **stringstream**, and then Save to other data types.
string s;
cin>>s;
stringstream ss(s);
int a;
string b;
ss>>a;
ss>>b;

sort

Skill

For simulation questions, we only need to consider two things, 1 how to store data 2 how to simulate

round() rounds

count() Find whether it exists in the hash table, the existence is 1

Sort ascending and descending order in the container

sort(Array.begin(),Array.end(),greater<类型>());  //降序
	sort(Array.begin(),Array.end(),less<类型>());   //升序

或者自定义cmp函数
bool cmp(int a,int b){
    
    
    return a>b;
}
sort(p[i].begin(),p[i].end(),cmp);

When using getline to read a line, if there is cin in front of it, you need to use getchar () to swallow the previous carriage return

getline and stringstream can traverse a string of strings separated by spaces

输入a b c d
string line;
getline(cin,line);
string key;
stringstream ss(line);
while(ss>>key) 

s = ss.substr(index); Intercept the string after the index position of the ss string and give it to s

Structure sorting overloaded operator

struct Student{
    
    
    string id;
    int score;
    int position;
    int localrank,allrank;
    bool operator<(const Student& t)const{
    
    
        if(score!=t.score) return score>t.score;
        else return id<t.id;
    }
}

After defining the structure, remember to write ;

heap sort

How to hand roll a pile?
First, the heap is a complete binary tree, and is divided into a small root heap (child nodes are smaller than the parent node) and a large root heap (child nodes are larger than the parent node).
So what is used to store in a heap? One-dimensional arrays!
For a one-dimensional array a[], n[1] represents the root node, and a[2 n] and a[2 n+1] are the left and right child nodes of a[n], respectively.
For a heap, we have two operations, the down() operation and the up() operation. The combination of these two operations (the time complexity is logn) can implement all modifications to the heap.
For small root heap:
**down()** operation: increase the value of a node, readjust the heap, move the node down
**up()** operation: reduce the value of a node, heap Re-jump, the node moves up the
image.png
heap sort: output the top of the heap each time
image.png

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int h[N],size_;
int n,m;
void down(int u){
    
    
    int t = u;
    if(2*u<=size_&&h[t]>h[2*u]) t = 2*u;
    if(2*u+1<=size_&&h[t]>h[2*u+1]) t = 2*u+1;  //两个if找出三个节点中最小的节点,用这个节点和父节点交换
    if(u!=t){
    
    
        swap(h[u],h[t]);
        down(t);
    }
}
int main(){
    
    
    cin>>n>>m;
    for(int i=1;i<=n;i++){
    
    
        int k;
        cin>>k;
        h[i] = k;
    } 
    size_ = n;
    for(int i=n/2;i;i--) down(i);
    while(m--){
    
    
        cout<<h[1]<<' ';
        swap(h[size_], h[1]);
        size_--;
        down(1);
    }
    return 0;
}

two points

Dichotomous template
1. The loop must be l < r
2. If the condition is judged to see if the condition is not met, then modify the upper and lower bounds
3. If r = mid - 1 after the if else, add 1 to the previous mid statement
4. Exit the loop It must be l == r, so either l or r can be used

There are only two cases for bisection
: 1: Find the first position greater than or equal to a given number (the first number that satisfies a certain condition)
2: Find the last number that is less than or equal to a given number (the last number that satisfies a certain condition)

image.png
image.png

// 判断条件很复杂时用check函数,否则if后直接写条件即可
bool check(int mid) {
    
    
    ...
    return ...;
}

// 能二分的题一定是满足某种性质,分成左右两部分
// if的判断条件是让mid落在满足你想要结果的区间内

// 找满足某个条件的第一个数  即右半段
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;
}

// 找满足某个条件的最后一个数  即左半段
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;
}


注释:
 模板2求mid时要加一,因为除以2默认是下取整
 比如红色区域二分到l = 3, r = 4,目标位置是4,那么 若不加一,则mid永远无法到4

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/124139425