2022 The third phase of the 14th Blue Bridge Cup simulation competition (problems and schedules)

The codes are all typed by myself, not necessarily correct ==

1. Minimal hexadecimal

Problem Description

Please find the smallest number greater than 2022 that, when converted to hexadecimal, has all digits (without leading 0s) as letters (A to F).
Please submit the decimal form of this number as an answer.

answer submission

This is a question of filling in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer, only fill in this integer when submitting the answer, filling in redundant content will result in no scoring.

reference answer

The 2730
violence is counted one by one, and you can save a few lines of code by using the sstream library function.

#include <iostream>
#include <sstream>

using namespace std;

int main() {
    
    
    for (int i = 2022;; i++) {
    
    
        ostringstream ss;
        ss << hex << i;
        string result(ss.str());
        bool flag = true;
        for (char j: result) {
    
    
            if (j < 'a' || j > 'z') {
    
    
                flag = false;
                break;
            }
        }
        if (flag) {
    
    
            cout << i << endl;
            break;
        }
    }
    return 0;
}


2. Columns in Excel

Problem Description

In Excel, column names use a combination of English letters. The first 26 columns use one letter, in order from A to Z, and the next 26*26 columns use a combination of two letters, in order from AA to ZZ.
What is the name of column 2022?

answer submission

This is a question of filling in the blanks with the result, you only need to calculate the result and submit it. The result of this question is a string composed of uppercase letters. When submitting the answer, only fill in this string. If you fill in the extra content, you will not get points.

reference answer

BYT
generates the names of all columns, and then directly takes the 2022th column

#include <iostream>
#include <sstream>
#include<vector>
#include <algorithm>

using namespace std;

int main() {
    
    
    vector<string> v;
    string temp = "A";
    for (int i = 0; i < 26; i++) {
    
    
        temp[0] = 'A' + i;
        v.push_back(temp);
    }
    temp = "AA";
    for (int i = 0; i < 26; i++) {
    
    
        temp[0] = 'A' + i;
        for (int j = 0; j < 26; j++) {
    
    
            temp[1] = 'A' + j;
            v.push_back(temp);
        }
    }
    temp = "AAA";
    for (int i = 0; i < 26; i++) {
    
    
        temp[0] = 'A' + i;
        for (int j = 0; j < 26; j++) {
    
    
            temp[1] = 'A' + j;
            for (int k = 0; k < 26; k++) {
    
    
                temp[2] = 'A' + k;
                v.push_back(temp);
            }
        }
    }
    cout << v[2021] << endl;
    return 0;
}


3. Equal dates

Problem Description

For a date, we can calculate the sum of the digits of the year, or the sum of the digits of the month and day. How many days are there in total from January 1, 1900 to December 31, 9999? The sum of the digits of the year is equal to the sum of the digits of the month plus the sum of the digits of the day.
For example, November 13, 2022 satisfies the requirement because 2+0+2+2=(1+1)+(1+3).
Please submit the total number of dates that meet the criteria.

answer submission

This is a question of filling in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer, only fill in this integer when submitting the answer, filling in redundant content will result in no scoring.

reference answer

70910
can be judged one by one

#include <iostream>
#include <sstream>
#include<vector>
#include <algorithm>

using namespace std;

int days[13] = {
    
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days2[13] = {
    
    0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
    
    
    int count = 0;
    for (int i = 1900; i <= 9999; i++) {
    
    
        for (int j = 1; j <= 12; j++) {
    
    
            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
    
    
                for (int k = 1; k <= days2[j]; k++) {
    
    
                    string s1 = to_string(i);
                    string s2 = to_string(j) + to_string(k);
                    int sum1 = 0, sum2 = 0;
                    for (char l: s1) sum1 += l - '0';
                    for (char l: s2) sum2 += l - '0';
                    if (sum1 == sum2) count++;
                }
            } else {
    
    
                for (int k = 1; k <= days[j]; k++) {
    
    
                    string s1 = to_string(i);
                    string s2 = to_string(j) + to_string(k);
                    int sum1 = 0, sum2 = 0;
                    for (char l: s1) sum1 += l - '0';
                    for (char l: s2) sum2 += l - '0';
                    if (sum1 == sum2) count++;
                }
            }
        }
    }
    cout << count << endl;
    return 0;
}


4. How many ways to take

Problem Description

Xiaolan has 30 numbers, namely: 99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77.
Xiaolan can take two numbers with different serial numbers from these numbers, and there are 30*29/2=435 ways to take them.
May I ask how many of these 435 methods yield the product of two numbers greater than or equal to 2022.

answer submission

This is a question of filling in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer, only fill in this integer when submitting the answer, filling in redundant content will result in no scoring.

reference answer

189

#include <iostream>
#include <sstream>
#include<vector>
#include <algorithm>

using namespace std;
int num[30] = {
    
    99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77};

int main() {
    
    
    int count = 0;
    for (int i = 0; i < 30; i++) {
    
    
        for (int j = i + 1; j < 30; j++) {
    
    
            if (num[i] * num[j] >= 2022) count++;
        }
    }
    cout << count << endl;
    return 0;
}


5. Maximum connected block

Problem Description

Xiaolan has a matrix of numbers with 30 rows and 60 columns, and each number in the matrix is ​​0 or 1. Two locations are said to be connected if one can walk from one location marked 1 to another location marked 1 through up, down, left, and right. All locations connected to a location marked 1 (including itself) form a connected block.
How big is the largest connected block in the matrix?

110010000011111110101001001001101010111011011011101001111110
010000000001010001101100000010010110001111100010101100011110
001011101000100011111111111010000010010101010111001000010100
101100001101011101101011011001000110111111010000000110110000
010101100100010000111000100111100110001110111101010011001011
010011011010011110111101111001001001010111110001101000100011
101001011000110100001101011000000110110110100100110111101011
101111000000101000111001100010110000100110001001000101011001
001110111010001011110000001111100001010101001110011010101110
001010101000110001011111001010111111100110000011011111101010
011111100011001110100101001011110011000101011000100111001011
011010001101011110011011111010111110010100101000110111010110
001110000111100100101110001011101010001100010111110111011011
111100001000001100010110101100111001001111100100110000001101
001110010000000111011110000011000010101000111000000110101101
100100011101011111001101001010011111110010111101000010000111
110010100110101100001101111101010011000110101100000110001010
110101101100001110000100010001001010100010110100100001000011
100100000100001101010101001101000101101000000101111110001010
101101011010101000111110110000110100000010011111111100110010
101111000100000100011000010001011111001010010001010110001010
001010001110101010000100010011101001010101101101010111100101
001111110000101100010111111100000100101010000001011101100001
101011110010000010010110000100001010011111100011011000110010
011110010100011101100101111101000001011100001011010001110011
000101000101000010010010110111000010101111001101100110011100
100011100110011111000110011001111100001110110111001001000111
111011000110001000110111011001011110010010010110101000011111
011110011110110110011011001011010000100100101010110000010011
010011110011100101010101111010001001001111101111101110011101

answer submission

This is a question of filling in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer, only fill in this integer when submitting the answer, filling in redundant content will result in no scoring.

reference answer

148
classic flood fill problem, dfs can search one by one. To avoid incomplete copy and paste, you can enter redirection

#include <iostream>
#include <sstream>
#include<vector>
#include <algorithm>

using namespace std;
string *matrix;
int temp;

void dfs(int x, int y) {
    
    
    if (x < 0 || y < 0 || x > 29 || y > 59 || matrix[x][y] == '0') return;
    matrix[x][y] = '0';
    temp++;
    dfs(x - 1, y);
    dfs(x + 1, y);
    dfs(x, y - 1);
    dfs(x, y + 1);
}


int main() {
    
    
    freopen("data.txt", "r", stdin);
    matrix = new string[30];
    for (int i = 0; i < 30; i++) {
    
    
        cin >> matrix[i];
    }
    int ans = 0;
    for (int i = 0; i < 30; i++) {
    
    
        for (int j = 0; j < 60; j++) {
    
    
            if (matrix[i][j] == '1') {
    
    
                temp = 0;
                dfs(i, j);
                ans = max(ans, temp);
            }
        }
    }
    cout << ans << endl;
    return 0;
}


6. What day

Problem Description

Given which day of the week a day is, what day of the week will it be after n days?

input format

The first line of input contains an integer w, indicating which day of the week the given day is, w is 1 to 6 for Monday to Saturday, and w is 7 for Sunday.
The second line contains an integer n.

output format

One line of output contains an integer indicating which day of the week is n days later, 1 to 6 represent Monday to Saturday, and 7 represents Sunday.

sample input

6
10

sample output

2

Evaluation use case scale and agreement

1 <= n <= 1000000 1 <= n <= 1000000 for all test cases1<=n<=1000000

reference answer

Just simulate

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    
    
    int a, b;
    cin >> a >> b;
    if ((a + b) % 7) {
    
    
        cout << (a + b) % 7 << endl;
    } else {
    
    
        cout << 7 << endl;
    }
    return 0;
}


7. Signal coverage

Problem Description

Xiaolan is responsible for the installation of signal towers in an area. The entire area is a rectangular area. After the coordinate axes are established, the coordinates of the southwest corner are (0, 0), the coordinates of the southeast corner are (W, 0), and the coordinates of the northwest corner are (0, H), and the coordinates of the northeast corner are (W, H). Where W, H are integers.
He set up signal towers at n positions, and each signal tower can cover a circle with a radius of R (including the edge) centered on itself.
In order to check the signal coverage, Xiaolan plans to test all the points in the area where the horizontal and vertical coordinates are integers to check the signal status. The abscissa ranges from 0 to W, the ordinate ranges from 0 to H, and a total of (W+1) * (H+1) points are tested.
Given the location of the signal tower, how many of these (W+1)*(H+1) points are covered by the signal.

input format

The first line of input contains four integers W, H, n, R, separated by a space between adjacent integers.
Next n lines, each line contains two integers x, y, representing the coordinates of a signal tower. Signal towers may overlap, indicating that two signal transmitters are installed in the same location.

output format

The output line contains an integer representing the answer.

sample input

10 10 2 5
0 0
7 0

sample output

57

Evaluation use case scale and agreement

For all evaluation cases, 1 <= W, H <= 100, 1 <= n <= 100, 1 <= R <= 100, 0 <= x <= W, 0 <= y <= H.

reference answer

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
int num[105][105] = {
    
    0};

inline double distance(int x1, int y1, int x2, int y2) {
    
    
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int main() {
    
    
    int W, H, n, R;
    cin >> W >> H >> n >> R;
    for (int i = 0; i < n; i++) {
    
    
        int x, y;
        cin >> x >> y;
        for (int j = x - R; j <= x + R; j++) {
    
    
            for (int k = y - R; k <= y + R; k++) {
    
    
                if (j >= 0 && j <= W && k >= 0 && k <= H && distance(x, y, j, k) <= R) {
    
    
                    num[j][k] = 1;
                }
            }
        }
    }
    int count = 0;
    for (int i = 0; i <= W; i++) {
    
    
        for (int j = 0; j <= H; j++) {
    
    
            if (num[i][j] == 1) {
    
    
                count++;
            }
        }
    }
    cout << count << endl;
    return 0;
}


8. Clean up the weeds

Problem Description

Xiaolan has a rectangular water area of ​​n * m size, Xiaolan divides this water area into n rows and m columns, the number of rows is from 1 to n, and the number of columns is from 1 to m. The width of each row and each column is unit 1.
Now, this water area is full of aquatic plants, and Xiaolan wants to clean up the aquatic plants.
Each time, Xiaolan can clean up a rectangular area, from column c1 (inclusive) to column c2 (inclusive) of row r1 (inclusive) to row r2 (inclusive).
After a period of cleaning, ask how many places have not been cleaned.

input format

The first line of input contains two integers n, m separated by a space.
The second line contains an integer t representing the number of cleanups.
In the next t lines, each line has four integers r1, c1, r2, c2. Adjacent integers are separated by a space, indicating a cleanup. Note the order of entries.

output format

The output line contains an integer representing the area that has not been cleaned.

Sample input 1

2 3
2
1 1 1 3
1 2 2 2

Sample output 1

2

Sample input 2

30 20
2
5 5 10 15
6 7 15 9

Sample output 2

519

Evaluation use case scale and agreement

For all evaluation cases, 1 <= r1 <= r2 <= n <= 100, 1 <= c1 <= c2 <= m <= 100, 0 <= t <= 100.

reference answer

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
int num[105][105] = {
    
    0};

int main() {
    
    
    int W, H, n;
    cin >> W >> H >> n;
    int count = 0;
    for (int i = 0; i < n; i++) {
    
    
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        for (int j = x1; j <= x2; j++) {
    
    
            for (int k = y1; k <= y2; k++) {
    
    
                if (num[j][k] == 0) {
    
    
                    num[j][k] = 1;
                    count++;
                }
            }
        }
    }
    cout << W * H - count << endl;
    return 0;
}


9. Longest slide

Problem Description

Xiaolan is going to slide in an open field. The height of the field is different. Xiaolan uses a matrix of n rows and m columns to represent the field, and the values ​​in the matrix represent the height of the field.
If Xiaolan is at a certain position, and the height of one of his up, down, left, and right positions is (strictly) lower than the current height, Xiaolan can slide there, and the sliding distance is 1.
If Xiaolan is at a certain position, and the heights of all his up, down, left, and right positions are greater than or equal to the current height, Xiaolan's skating is over.
Xiaolan cannot slide out of the field represented by the matrix.
Xiaolan can choose any position to start gliding. May I ask how far Xiaolan can slide at most.

input format

The first line of input contains two integers n, m separated by a space.
The next n lines, each line contains m integers, separated by a space between adjacent integers, indicating the height of each position in turn.

output format

The output line contains an integer representing the answer.

sample input

4 5
1 4 6 3 1
11 8 7 3 1
9 4 5 2 1
1 3 2 2 1

sample output

7

Sample description

The sliding position is (2, 1), (2, 2), (2, 3), (3, 3), (3, 2), (4, 2), (4, 3) at a time.

Evaluation use case scale and agreement

For 30% 30\%30% of evaluation cases,1 <= n <= 20 1 <= n <= 201<=n<=20 1 < = m < = 20 1 <= m <= 20 1<=m<=20 ,0 <= height <= 100 0 <= height <= 1000<=high<=100 . 1 <= n <= 100 1 <= n <= 100
for all test cases1<=n<=100 1 < = m < = 100 1 <= m <= 100 1<=m<=100 ,0 <= height <= 10000 0 <= height <= 100000<=high<=10000

reference answer

The classic memory search question, remember that there seems to be this question in the school OJ?
Use a two-dimensional array to store the longest path of each point. If the longest path of this point has been calculated in the dfs process, it will be taken directly. If not, calculate it again

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
int n, m;
int num[105][105] = {
    
    0};
int dp[105][105] = {
    
    0};
int dir[4][2] = {
    
    {
    
    0, 1}, {
    
    0, -1}, {
    
    1, 0},{
    
    -1, 0}};

int dfs(int i, int j) {
    
    
    if (dp[i][j] != -1) return dp[i][j];
    int ret = 1;
    for (int k = 0; k < 4; k++) {
    
    
        int x = i + dir[k][0];
        int y = j + dir[k][1];
        if (x >= 1 && x <= n && y >= 1 && y <= m && num[x][y] < num[i][j]) {
    
    
            ret = max(ret, dfs(x, y) + 1);
        }
    }
    dp[i][j] = ret;
    return dp[i][j];
}

int main() {
    
    
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
    
    
        for (int j = 1; j <= m; j++) {
    
    
            cin >> num[i][j];
            dp[i][j] = -1;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
    
    
        for (int j = 1; j <= m; j++) {
    
    
            ans = max(ans, dfs(i, j));
        }
    }
    cout << ans << endl;
    return 0;
}


10. Interval Minimum

Problem Description

Xiaolan has a sequence a[1], a[2], ..., a[n].
Given a positive integer k, for each serial number i between 1 and n, what is the number of a[ik], a[i-k+1], …, a[i+k] among the 2k+1 numbers What is the minimum value? When a subscript exceeds the range from 1 to n, the number does not exist, and only those values ​​that exist are used when calculating the minimum value.

input format

The first line of input contains an integer n.
The second line contains n integers representing a[1], a[2], …, a[n] respectively.
The third line contains an integer k.

output format

Output one line, containing n integers, representing the minimum value obtained for each sequence number respectively.

sample input

5
5 2 7 4 3
1

sample output

2 2 2 3 3

Evaluation use case scale and agreement

For 30% 30\%30% of the evaluation cases,1 <= n <= 1000 1 <= n <= 10001<=n<=1000 1 < = a [ i ] < = 1000 1 <= a[i] <= 1000 1<=a[i]<=1000 .
For50% 50\%50% of the evaluation cases,1 <= n <= 10000 1 <= n <= 100001<=n<=10000 1 < = a [ i ] < = 10000 1 <= a[i] <= 10000 1<=a[i]<=10000 . 1 <= n <= 1000000 1 <= n <= 1000000
for all test cases1<=n<=1000000 1 < = a [ i ] < = 1000000 1 <= a[i] <= 1000000 1<=a[i]<=1000000

reference answer

RMQ questions classic board questions, ST algorithm

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 1e6 + 10, M = 20;

int n, k, t;
int q[N];
int f[N][M];

int query(int l, int r) {
    
    
    int len = log(r - l + 1) / log(2);
    int x = f[l][len], y = f[r - (1 << len) + 1][len];
    return q[x] > q[y] ? y : x;
}

int main() {
    
    
    cin >> n;
    for (int i = 1; i <= n; i++) scanf("%d", &q[i]);
    cin >> k;
    t = log(n) / log(2);
    for (int j = 0; j <= t; j++) {
    
    
        for (int i = 1; i + (1 << j) - 1 <= n; i++) {
    
    
            if (!j) f[i][j] = i;
            else {
    
    
                int l = f[i][j - 1], r = f[i + (1 << (j - 1))][j - 1];
                if (q[l] > q[r]) f[i][j] = r;
                else f[i][j] = l;
            }
        }
    }
    int l, r;
    for (int i = 1; i <= n; i++) {
    
    
        l = max(1, i - k), r = min(n, i + k);
        cout << q[query(l, r)] << " ";
    }
    cout << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_46326495/article/details/129273435