Key points of the Blue Bridge Cup (C/C++) (updated at any time)

Pay attention to not get lost, welcome to recommend to more people

Table of contents

1 trick

1.1 Cancel synchronization (save time, even cheat points, it is best to write every program)

1.2 Universal library (may delay compilation time, but save your brain)

1.3 Don't forget to write the return 0 of the Blue Bridge Cup! !

1.4 Compile Settings (Dev C++)

1.5 memset filling function

1.6 Time Complexity

1.6.1 Constant order O(1)

1.6.2 Logarithmic order O(logn)

1.6.3 Linear order O(n)

1.6.4 Linear-logarithmic order O(nlogn)

1.6.5 Multiple loops O(n^k)

1.7 Pruning

1.8 find function

2 Basic algorithms and techniques

2.1 BFS (Breadth First Search)

2.2 DFS (Depth First Search)

2.3 Greatest common divisor and least common multiple

2.4 Binary conversion

2.4.1 Decimal as the medium (common type)

2.4.2 Binary as the medium (trick type)

2.5 Binary representation

2.6 The knapsack problem

2.6.1 01 knapsack problem

2.6.2 Multiple knapsack problem (multiple items of each item)

2.6.3 Complete knapsack problem (infinite number of each item)

2.7 Dynamic Programming (DP)

2.8 Greed

2.9 Divide and Conquer (updated later)

2.10 Splitting numbers into arrays

2.11 Mutualization of numbers and strings

2.12 Sorting

3  STL

3.1 Queue (queue)

3.2 Linked list (list) 

 3.3 priority queue (priority queue)

3.4 Vector/dynamic array (vector)

 3.5 stack (stack)

3.6 Collection (set) (requires no repeated elements)

 3.7 collection/map/key-value pair (map)

3.8 Iterators


1 trick

1.1 Cancel synchronization (save time, even cheat points, it is best to write every program)

ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

1.2 Universal library (may delay compilation time, but save your brain)

#include <bits/stdc++.h>

1.3 Don't forget to write the return 0 of the Blue Bridge Cup! !

1.4 Compile Settings (Dev C++)

(1) Tools -> Compilation Options -> Compiler -> Add the following command when compiling -> Adjust to C99

 (2) Tools -> Compilation Options -> Code Generation/Optimization -> Code Generation -> Language Standard

1.5 memset filling function

Initialize the memory block by byte, note that only 0 or -1 can be filled

#include <bits/stdc++.h>
using namespace std;
int a[10];
int main()
{
	memset(a,-1,sizeof(a));
	for(int i=0;i<10;i++)
	{
		cout<<a[i]<<endl;
	}
	return 0;
}

1.6 Time Complexity

The compilation time of each question in the Blue Bridge Cup is limited to less than 1s. When encountering a question with relatively large data, it is often necessary to reduce the time complexity.

It is roughly estimated that about 400 million times are completed in one second in the case of O(n), about 20,000 times in one second in the case of O(n*n), and about 700 times in the case of O(n*n*n).

Since the Blue Bridge Cup evaluation system is based on the number of passing samples, we must pay attention to the value range of the samples when doing the questions.

Example: K-fold interval (the brute force method can only pass some samples, so a better algorithm should be used)

(1 message) K-fold interval (the tenth question of Group B of the 8th Provincial Competition of the Blue Bridge Cup in 2017) (C/C++)_Caizhiyin's Blog-CSDN Blog https://blog.csdn.net/ m0_71934846/article/details/128434135?spm=1001.2014.3001.5501

1.6.1 Constant order O(1)

int i=1;
int j=2;
int m=i+j;

1.6.2 Logarithmic order O(logn)

int i=1;
while(i<n)
{
  i=i*2;
}

1.6.3 Linear order O(n)

for(int i=0;i<n;i++)
{
  cout<<i<<endl;
)

1.6.4 Linear-logarithmic order O(nlogn)

for(int m=1;m<n;m++)
{
  int i=1;
  while(i<n)
  {
    i=i*2;
  }
}

1.6.5 Multiple loops O(n^k)

k is the number of loop layers

1.7 Pruning

Don’t let the computer calculate the values ​​that are impossible to obtain when doing the questions. Try to save time. The ones encountered in the Blue Bridge Cup have not been used too cumbersome pruning, and most of them are also in BFS and DFS. appears in (bool vis)

1.8 find function

Function: Find the address of the first occurrence of the element in the array (similar to the address of 0x)

Model: find (find start point, find end point, find target element)

Similarly, the search interval is [find start point, find end point)

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int a[10]={2,6,8,1,3,7,5,1,0,11};
	cout<<find(a+0,a+5,8)<<endl;//打印类似0x地址 
	cout<<find(a+0,a+5,8)-a<<endl;//打印数组【】内的序号 
	return 0;
}

1.9 PI problems

PI=atan(1.0)*4

2 Basic algorithms and techniques

2.1 BFS (Breadth First Search)

Queues are used (sometimes priority queues are used)

The main idea: push all eligible points into the queue, and then pop up one by one to search up, down, left, right, front and back, until the queue is empty, which means the search is complete. When searching, pay attention to judging whether it has been searched, and use bool vis【】to judge.

Example Topic: Global Warming

(1 message) Global warming (Lanqiao Cup 2018 provincial competition group B test questions I) (C/C++)_Caizhiyin's Blog-CSDN Bloghttps: //blog.csdn.net/m0_71934846/article/ details/128434254?spm=1001.2014.3001.5502

2.2 DFS (Depth First Search)

Use recursion (not easy to understand)

Main template: please refer to the following full arrangement examples

http://t.csdn.cn/ANnS1

To sum up, there are the following steps:

(1) Determine the boundary if () return;

(2) Enter the for loop

(3) Determine whether you have searched if (vis[]) vis[]=true; dfs(); vis[]=false;

Example: Calculation formula

(3 messages) Calculation formula (the third question of the Blue Bridge Cup 2016 Provincial Competition Group B) (C/C++)_Caizhiyin's Blog-CSDN Bloghttps: //blog.csdn.net/m0_71934846/article/ details/128434004?spm=1001.2014.3001.5502

2.3 Greatest common divisor and least common multiple

Greatest common divisor (gcd)

#include <bits/stdc++.h>
using namespace std;

int main()
{
	cout<<__gcd(25,5);
	return 0;
}

Least common multiple (lcm)

Write one more lcm function

#include <bits/stdc++.h>
using namespace std;

int lcm(int a,int b)
{
    return a*b/__gcd(a,b);
}
int main()
{
	cout<<lcm(25,5);
	return 0;
}

2.4 Binary conversion

2.4.1 Decimal as the medium (common type)

(8 messages) Convert any base to decimal/decimal to any base (ASCII code method) (C/C++ ) article/details/128297645?spm=1001.2014.3001.5502

2.4.2 Binary as the medium (trick type)

(2 messages) Hexadecimal to octal (Lanqiao Cup basic practice C/C++) _ dish only because of C's blog - CSDN blog https://blog.csdn.net/m0_71934846/article/details/128745875?csdn_share_tail =%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22128745875%22%2C%22source%22%3A%22m0_71934846%22% 7D

2.5 Binary representation

Example: boring funny

(8 messages) Algorithm training for Lanqiao Cup test questions is boring (C/C++)_Cooking because of C's blog-CSDN blog https://blog.csdn.net/m0_71934846/article/details/128717938?spm=1001.2014 .3001.5502

2.6 The knapsack problem

2.6.1 01 knapsack problem

#include<bits/stdc++.h>

using namespace std;

int v[1000];    // 体积
int w[1000];    // 价值 
int f[1000][1000];  // f[i][j], j体积下前i个物品的最大价值 

int main() 
{
    int n, m;   
    cin >> n >> m;
    for(int i = 1; i <= n; i++) 
        cin >> v[i] >> w[i];

    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= m; j++)
        {
            //  当前背包容量装不进第i个物品,则价值等于前i-1个物品
            if(j < v[i]) 
                f[i][j] = f[i - 1][j];
            // 能装,需进行决策是否选择第i个物品
            else    
                f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i]);
        }           

    cout << f[n][m] << endl;

    return 0;
}

2.6.2 Multiple knapsack problem (multiple items of each item)

Knead multiple items into a new item, and stack them up according to the serial number

2.6.3 Complete knapsack problem (infinite number of each item)

#include<iostream>
using namespace std;
const int N = 1010;
int f[N];
int v[N],w[N];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i = 1 ; i <= n ;i ++)
    {
        cin>>v[i]>>w[i];
    }
 
    for(int i = 1 ; i<=n ;i++)
    for(int j = v[i] ; j<=m ;j++)
    {
            f[j] = max(f[j],f[j-v[i]]+w[i]);
    }
    cout<<f[m]<<endl;
}

2.7 Dynamic Programming (DP)

Example: take gold coins

(1 message) Take Gold Coins (Lanqiao Cup Algorithm Training) (C/C++)_Caizhicai's Blog-CSDN Bloghttps://blog.csdn.net/m0_71934846/article/details/128456365?spm= 1001.2014 .3001.5502

2.8 Greed

Idea: choose a local optimal solution, but the biggest flaw is that it is not applicable in some cases

Example: Paper Money Problem

For example, there are denominations of 1 yuan, 2 yuan, 5 yuan, 10 yuan, 20 yuan, 50 yuan, and 100 yuan. Then for 110 yuan, you can use greed to find from the largest denomination of 100 yuan.

But if you change the denomination of banknotes, such as 1 yuan, 2 yuan, 5 yuan, 20 yuan, 55 yuan, 100 yuan, then if you use the greedy algorithm, you will find that the optimal solution cannot be found (greedy: 100+5+5= 110 dynamic programming: 55+55=110)

2.9 Divide and Conquer (updated later)

mostly dichotomous

2.10 Splitting numbers into arrays

(2 messages) Split and store a number into an array (C/C++ ) .3001.5501

2.11 Mutualization of numbers and strings

Can't find subnumbers, but can find substrings

Example: Super prime numbers

(2 messages) Super Prime Numbers (Lanqiao Cup C/C++ Algorithm Competition)_Caizhiyin's Blog-CSDN Bloghttps://blog.csdn.net/m0_71934846/article/details/128723978?csdn_share_tail=%7B % 22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22128723978%22%2C%22source%22%3A%22m0_71934846%22%7D

2.12 Sorting

(4 messages) Finding the largest character in a string (three quick methods)_Finding the largest character in a string_Food only because of C's blog-CSDN blog https://blog.csdn.net/m0_71934846/article/details /128457227?spm=1001.2014.3001.5501

3  STL

3.1 Queue (queue)

3.2 Linked list (list) 

 3.3 priority queue (priority queue)

 The priority queue defaults to a large root heap (big to small sort), if you want to sort from small to large, then

<int, vector<int>, greater<int> >//Arrange in ascending order (small root heap)

<int,vector<int>,less<int> >//sort in descending order (big root heap)

3.4 Vector/dynamic array (vector)

 3.5 stack (stack)

3.6 Collection (set)  (requires no repeated elements)

set<int> s;//default ascending order

set<int, greater<int>>  s2 = {3,2,5,1,4 ,3};//降序

The set value cannot be modified (the data order cannot be guaranteed after modification)

 3.7 collection/map/key-value pair (map)

3.8 Iterators

 template:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(11);
	v.push_back(7);
	vector<int>::iterator it = v.begin();
	
	while(it!=v.end())
	{
		cout << *it <<"  ";
		it++;
	}
	cout << endl;
	return 0;
}


Here is a general list of the knowledge points and skills that need to be mastered to participate in the Blue Bridge Cup. If you want to learn more about a certain knowledge point, you can read my sample questions and other people's articles

Guess you like

Origin blog.csdn.net/m0_71934846/article/details/128721132