CSP-J 2019 Entry Level First Round Question 17

【topic】

CSP-J 2019 Entry Level First Round Question 17
Reading Procedure

#include<cstdio>
using namespace std;
int n, m;
int a[100], b[100];

int main() {
    
    
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        a[i] = b[i] = 0;
    for (int i = 1; i <= m; ++i) {
    
    
        int x, y;
        scanf("%d%d", &x, &y);
        if (a[x] < y && b[y] < x) {
    
    //第13行
            if (a[x] > 0)
                b[a[x]] = 0;//第15行
            if (b[y] > 0)
                a[b[y]] = 0;
            a[x] = y;
            b[y] = x;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
    
    
        if (a[i] == 0)
            ++ans;
        if (b[i] == 0)
            ++ans;//第27行
    }
    printf("%d", ans);
    return 0;
}

Assuming that the input n and m are both positive integers, and x and y are integers in the range of [1,n], complete the following true/false questions and multiple-choice questions: true/false
questions

  1. When m>0, the output value must be less than 2n. ()
    A. True
    B. False
  2. After executing ++ans on line 27, ans — must be an even number. ()
    A. True
    B. False
  3. a[i] and b[i] cannot both be greater than 0. ()
    A. True
    B. False
  4. When the right program is executed to line 13, x is always less than y, then line 15 will not be executed. ()
    A. True
    B. False
    Multiple choice
  5. If m x are different in pairs, and m y are different in pairs, the output value is ()
    A. 2n-2m
    B. 2n+2
    C. 2n-2
    D. 2n
  6. If the m x's are different in pairs, and the m y's are equal, the output value is ()
    A. 2n-2
    B. 2n
    C. 2m
    D. 2n-2m

【Title test site】

1. Array

2. Mapping (correspondence)

【Things to solve the problem】

See the first paragraph:

int n, m;
int a[100], b[100];
int main() {
    
    
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        a[i] = b[i] = 0;

It is easy to know that a and b are two integer arrays with an initial value of 0. n is the number of elements in the array.
Look at the second for loop:

for (int i = 1; i <= m; ++i) {
    
    
	int x, y;
	scanf("%d%d", &x, &y);
	if (a[x] < y && b[y] < x) {
    
    
		if (a[x] > 0)
			b[a[x]] = 0;
		if (b[y] > 0)
			a[b[y]] = 0;
		a[x] = y;
		b[y] = x;
	}
}

Input a pair of numbers x, y each time in the loop, we can see that m is the logarithm of the input pair.
Next, you can observe the law through the simulation running method

In the initial state, the initial values ​​of a and b are both 0

i 1 2 3 4
a[i] 0 0 0 0
b[i] 0 0 0 0
  1. If 1 2 is entered for the first time, then x: 1, y: 2 The
    initial values ​​of the a and b arrays are all 0, and a[x]<y, b[y]<x are all true.
    It is judged that a[x]>0 and b[y]>0 are both false, and the statement under if is not executed.
    Do a[x]=y, b[y]=x, then a[1]:2, b[2]:1.
i 1 2 3 4
a[i] 2 0 0 0
b[i] 0 1 0 0
  1. If you enter 2 3 for the second time, then x: 2, y: 3
    a[x]<y, b[y]<x are all true, enter the if statement.
    Both a[x]>0 and b[y]>0 are false, and the statement under the if inside is not executed.
    Execute a[x]=y, b[y]=x, then a[2]:3, b[3]:2
i 1 2 3 4
a[i] 2 3 0 0
b[i] 0 1 2 0
  1. If you enter 1 3 for the third time, then x: 1, y: 3
    a[x]<y is true, b[y]<x is false, the judgment fails, and the statement in the if is not executed.

  2. If you enter 1 4, then x: 1, y: 4
    a[x]<y, b[y]<x are all true, enter the if statement.
    Currently a[x]>0, run b[a[x]]=0, ie b[2]=0.
    b[y]=0, the condition is not met.
    Then execute a[x]=y, b[y]=x, so that: a[1]:4, b[4]:1

i 1 2 3 4
a[i] 4 3 0 0
b[i] 0 1 2 1

So far, you can basically see the function of this code.
a[x]=y, b[y]=xThat is, a mapping, or a set of one-to-one correspondences, is determined.
It can be imagined that there is a column of numbers on the left and a column of numbers on the right.
Input x, y, that is, let the number x on the left correspond to the number y on the right, and connect the two.
a[x]The x on the left corresponds to the number b[y]on the right, and the y on the right corresponds to the number on the left.
In the above example, after entering 1 2, 2 3, the situation is as follows
insert image description here
. After entering 1 3, the situation remains unchanged.
Then after entering 1 4, the situation is as follows:

insert image description here
It can be seen that the premise of changing the connection of the numbers on both sides is that the new numbers connected by the numbers on both sides are larger than the previously connected numbers.

For example, if you already have connections 1-4, 2-3
, if you want to connect 1-3, you can't, because the number connected to 1 on the left becomes smaller, and the number connected to 3 on the right also becomes smaller.
If you want to connect 1–2, although the right 2 is never connected (the number of connections is 0) to 1, the number of the left 1 connection becomes smaller, which is not allowed.
Then you can connect 2–4, the number connected by 2 on the left changes from 3 to 4, and the number connected by 4 on the right changes from 1 to 2, all of which become larger, which is okay. After the modification, the 1 on the left and the 3 on the right are no longer connected to numbers (it can also be said that the connected numbers are 0).

 int ans = 0;
 for (int i = 1; i <= n; ++i) {
    
    
     if (a[i] == 0)
         ++ans;
     if (b[i] == 0)
         ++ans;
 }
 printf("%d", ans);

The last paragraph is to count the number of numbers that are not involved in the connection in the two columns of numbers.

For example, when n is 4 in the above figure, when only 1–4, 2–3 are connected, the number of numbers not involved in the connection is 4

So far, I have understood that the function of the entire code is: there are two columns of n numbers, input m number pairs, and connect the corresponding numbers in the two columns. If a new connection is made, the numbers on both sides of the connection will be connected to a larger number. , the connection is updated. Keep each number involved in at most 1 connection. Finally, count the number of numbers that did not participate in the connection.

1. When m>0, the output value must be less than 2n. ()
A. Correct
B. Error

m>0, that is, there are several pairs to be connected. Two columns of numbers, one column has n numbers, the total number of numbers is 2n, and the number of unconnected numbers after the connection must be less than 2n, choose A, which is correct.
2. After executing ++ans on line 27, ans — must be an even number. ()
A. True
B. False

The ++ans on line 27 are

 if (b[i] == 0)
    ++ans;

++ans in .
After executing this sentence, it does not mean that the entire for loop will end. This is just one step in the statistical process. After the entire statistics is over, the number of unconnected numbers must be even, but there is no guarantee that ans must be even during the statistics process. So the question is B, which is wrong.

For example, if n is 2, enter 1 2. There are only 1–2 lines in the two columns of numbers, when i is 1, a[1] is 2, b[1] is 0, running ++ans, ans is 1. In this case, ans is an odd number.

3. a[i] and b[i] cannot be greater than 0 at the same time. ()
A. Correct
B. Wrong

If the i-th number on the left and the i-th number on the right participate in the connection at the same time, a[i] and b[i] can be greater than 0 at the same time. Choice B is wrong.

For example, if n is 2, enter 1 2, and then enter 2 1. There are 1-2 and 2-1 in the two columns of numbers, then a[1] is 2, b[1] is 2, and both are greater than 0.

4. When the right program is executed to line 13, x is always less than y, then line 15 will not be executed. ()
A. True
B. False

if (a[x] < y && b[y] < x) {
    
    //第13行
    if (a[x] > 0)
        b[a[x]] = 0;//第15行
    if (b[y] > 0)
        a[b[y]] = 0;
    a[x] = y;
    b[y] = x;
}

Line 15 executes the condition that the number x on the left is connected to a number y that is greater than a[x] on the right. It turns out that the right side a[x] naturally does not need to connect numbers, so the number it connects to is set to 0.
So the condition executed on line 15 is not x<y, but a[x]<y && a[x]>0.
Choice B is wrong.
5. If m x pairs are different, and m y pairs are different, the output value is ()
A. 2n-2m
B. 2n+2
C. 2n-2
D. 2n

If each input xy is in : x is different, y is different. Then each time the two numbers that are not connected on both sides will be connected. A total of m pairs of numbers will be connected, the number of unconnected numbers will be reduced by 2m, and the total number of numbers will be 2n, so the number of remaining unconnected numbers is 2n-2m, choose A.
6. If the m x's are different in pairs, and the m y's are all equal, the output value is ()
A. 2n-2
B. 2n
C. 2m
D. 2n-2m

m y's are all equal, and finally y can only be Connect with 1 number on the left, the unconnected number is 2n-2, choose A.

【Answer】

  1. A
  2. B
  3. B
  4. B
  5. A
  6. A

Guess you like

Origin blog.csdn.net/lq1990717/article/details/126901236