Individual competition of the 9th "Turing Cup" NEUQ-ACM Programming Competition

Posted by lansing on Tue, 18 Jan 2022 18:58:17 +0100

A. Current situation at the end of University

Title Description

As a college student, it's time to check your final grade again. When your grade is greater than or equal to 60, please output "jige,haoye!", Otherwise, output "laoshi,caicai,laolao".

Enter Description:

One line, an integer x represents your score (0 < = x < = 100)

Output Description:

One line string

Example 1

input

60

output

jige,haoye!

code:

#include <iostream>
using namespace std;

int main() {
	int x;
	cin >> x;
	if (x >= 60)
		cout << "jige,haoye!";
	else
		cout << "laoshi,caicai,laolao";
	return 0;
}

B.G1024

Title Description
After the final examination, several students in the Turing Cup team humbly stayed in school to write questions, but due to the impact of the epidemic, they had to take train G1024 to leave school as soon as possible. Now suppose that there are nnn people in the Turing Cup team, and you can know the number of purchased votes xxx and total votes mmm in G1024 in the next kkk days through APP, Now Phenix wants to know the earliest day to leave school on the premise that everyone gets on the train together. If you can't leave anyway, please output "G!"

Enter Description:

Two integers n,kn,kn,k in the first line represent the number of people and days of the problem group (n, K < 1000) (n, K < 1000) (n, K < 1000)

In the next kkk line, the two integers x,mx,mx,m in line iii represent the number of purchased tickets and the total number of tickets (0 < = x < = m < 1000) (0 < = x < = m < 1000) (0 < = x < = m < 1000)

Output Description:

On the premise that everyone gets on the train together, you can leave school as early as the first day. If you can't leave, please output "G!"

Example 1

input

7 5
100 100
99 100
95 100
900 1000
0 1000

output

4

code:

#include <bits/stdc++.h>
using namespace std;
int n, k, ans;

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= k; i ++) {
		int x, y;
		scanf("%d%d", &x, &y);
		if (y - x >= n && !ans) {
			ans = i;
		}
	}
	if (ans)
		printf("%d\n", ans);
		puts("G!");
	return 0;
}

C.NEUQ

Title Description
One day, Phenix got a string with length of nnn. The string is only composed of capital letters A~Z. now Phenix wants to know how many characters need to be deleted at least to make the string become NEUQNEUQ... This form composed of several "NEUQ".

Enter Description:

The first line is an integer nn, indicating the length of the string (n < = 10 ^ 6N < = 106)
The second line is a string

Output Description:

An integer representing the minimum number of characters to delete

Example 1

input

10
NEUUQNEUQQ

output

2

Example 2

input

9
ILOVENEUQ

output

5

code:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;

char S[N], T[] = {'N', 'E', 'U', 'Q'};

int main() {
	int n;
	scanf("%d", &n);
	cin >> S;
	int cnt = 0, now = 0;
	for (int i = 0; i < n; i ++) {
		if (T[now] == S[i]) {
			now++;
			if (now == 4)
				cnt++, now = 0;
		}
	}
	printf("%d\n", n - 4 * cnt);
	return 0;
}

D. Little G's task

Title Description

After checking the questions, Phenix felt that there were too many simple questions in the Turing cup, which was not in line with the original intention of the Turing cup to test the algorithm programming ability. She decided to add a more difficult question and handed over the task to Xiao G.

As we all know, little G's level is very limited. At present, he can't create difficult problems, so he plans to go to the major oj inside for white whoring.

At present, xiaog can find a total of nnn OJS. For the third oj, we can define the number of appropriate questions with white whoring difficulty as aia_iai​ ,aia_ The size of IAI is defined as the sum of the digits of the number i.

For example, I = 233, ai=2+3+3=8i = 233, a_i = 2 + 3 + 3 = 8i=233,ai​=2+3+3=8.

 i=2048,ai=2+0+4+8=14i = 2048 , a_i = 2 + 0 + 4 + 8 = 14i=2048,ai​=2+0+4+8=14 .

Now given that there are nnn OJS who can whore for nothing, little G wants to know how many questions he can whore for nothing in the end.
That is, find Σ i=1nai\sum_{i = 1}^{n} a_i∑i=1n​ai​.
 

Enter Description:

A positive integer n < = 1000000n < = 1000000

Output Description:

A positive integer indicates how many questions can be ignored

Example 1

input

9

output

45

Example 2

input

99

output

900

code:

#include <cstdio>
#include <iostream>
using namespace std;
#define int long long
int sum ;
int n ;
int ans ;

signed main() {
	cin >> n ;
	for (int i = 1 ; i <= n ; ++i) {
		int x = i ;
		while (x)
			ans += x % 10, x /= 10 ;
	}
	cout << ans << endl ;
}

E.nn and games

Title Description
nn was suddenly interested in playing games recently, so he went to xf to ask about related topics, and xf was just doing a game demo.
At present, there is an n in the game × nn\times nn × n-size map with several controllable friendly units, the same number of hostile units and some obstacles. In order to fully achieve the restraint of each unit, players need to manually control each different unit to move to attack near the enemy unit they restrain.
Now xf forcibly shoves the implementation of such a function to nn: players need to know immediately whether all units they can control can bypass various obstacles and move near their restrained local units.
Can you help nn solve this problem?

Enter Description:

On line 111, enter nnn, mmm, indicating that the map size is n × nn\times nn × n. There are MMM obstacles.
In line 2 ∼ m+12\sim m+12 ∼ m+1, input x, YX, YX and Y in turn in each line to represent the coordinate position of each obstacle.
Enter ttt in line m+2m+2m+2, representing a total of ttt controllable units and ttt hostile units.
In line m+3m+3m+3, enter x1, y1, x2, y2x for each subsequent line_ 1,y_ 1,x_ 2,y_ 2x1, y1, x2, y2, (x1,y1)(x_1, y_1)(x1, y1) represents the position of a friendly controllable unit, (x2,y2)(x_2,y_2)(x2, y2) represents the position of an enemy unit restrained by it (i.e. the target point of the controllable friendly unit).
The data ensures that no unit and obstacle will overlap, and the x, yx, yx and y coordinates of the map are within the range of [0, n) [0, n) [0, n].
1≤n≤1031\le n\le 10^31≤n≤103,0≤m<1060\le m< 10^60≤m<106
1≤t≤101\le t\le 101≤t≤10,0≤x,y,x1,y1,x2,y2<n0\le x, y, x_1, y_1, x_2, y_2< n0≤x,y,x1​,y1​,x2​,y2​<n

Output Description:

Output whether each controllable unit can reach the position of the corresponding enemy unit according to the input sequence of controllable unit coordinates. If it can, output the shortest steps required to reach the corresponding position, otherwise output - 1.
Note: it is necessary to consider the blocking between units during pathfinding (that is, a unit, whether controllable or hostile, will become an obstacle to other units), but because it is only a matter of calculating a time point, it is assumed that other units are stationary in their original position during calculation.

Example 1

input

3 1
1 0
2
0 0 1 1
0 1 2 2

output

-1
3

explain

The map is as follows (a and b are controllable units, the corresponding restrained hostile units are c and d, the obstacle is 1, and the empty position is 0)

00d bc0 a10
As shown in the figure, the distance from a to c is blocked by b and 1, so - 1 is output; b can reach d in three steps: up, right and right. This path is the shortest path for b to reach d, so output 3

code:

#include <bits/stdc++.h>
#define MS(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
using namespace std;

struct Point {
	int x, y, d;
	Point(int _x, int _y, int _d = 0): x(_x), y(_y), d(_d) {}
	bool operator==(const Point &a) {
		return x == a.x && y == a.y;
	}
};

int dx[4] = {0, 0, 1, -1};

int dy[4] = {1, -1, 0, 0};
bool M1[1005][1005], M2[1005][1005];
vector<Point> warrior[3];
int n;

int bfs(const Point &s, const Point &t) {
	MS(M2, false);
	queue<Point>q;
	q.push(s);
	while (!q.empty()) {
		Point p = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int x = p.x + dx[i];
			int y = p.y + dy[i];
			if (x < 0 || x >= n || y < 0 || y >= n || M1[x][y] || M2[x][y])
				continue;
			M2[x][y] = true;
			Point tmp = Point(x, y, p.d + 1);
			if (tmp == t)
				return tmp.d;
			q.push(tmp);
		}
	}
	return -1;
}

void solve() {
	int m;
	MS(M1, false);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y;
		cin >> x >> y;
		warrior[0].push_back(Point(x, y));
		M1[x][y] = true;
	}
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		warrior[1].push_back(Point(x1, y1));
		M1[x1][y1] = true;
		warrior[2].push_back(Point(x2, y2));
		M1[x2][y2] = true;
	}
	for (int i = 0; i < warrior[1].size(); i++) {
		M1[warrior[2][i].x][warrior[2][i].y] = false;
		cout << bfs(warrior[1][i], warrior[2][i]) << endl;
		M1[warrior[2][i].x][warrior[2][i].y] = true;
	}
}

int main() {
	solve();
	return 0;
}

F. Second largest number

Title Description
The bull God was tired of the first throne and began to study the second greatest contribution.
Now give you an permutation PPP of NNN numbers, including (1,2,...,N)(1,2,...,N)(1,2,...,N), where XL and Rx are defined for any given group (L,R)(L,R)(L,R) (1 ≤ l < R ≤ N) (1 \ Le l < R \ Le N) (1 ≤ l < R ≤ N)_ {L, R} XL, R} is the second largest number of sequence elements from subscript LLL to RRR.
Now, please be smart and calculate Σ L=1N − 1 Σ r = L + 1nxl, R \ sum_ {L=1}^{N-1}\sum_ {R=L+1}^NX_ As a result of {L, R} ∑ L=1N − 1 ∑ R=L+1N} XL,R}, AC can obtain a signed photo of the cow God by luck.

Enter Description:

The first line is the number NNN that Niu shen wants to study, where 2 ≤ N ≤ 1042 \le N \le10^{4}2 ≤ N ≤ 104.
The second line is an arrangement of NNN.

Output Description:

The output includes an integer representing the result.

Example 1

input

3
2 3 1

output

5

code:

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

ll arr[10100];
int main() {
	ll n, ans = 0, first, second;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i < n; i++) {
		first = max(arr[i], arr[i + 1]);
		second = min(arr[i], arr[i + 1]);
		ans += second;
		for (int j = i + 2; j <= n; j++) {
			if (arr[j] > first) {
				second = first;
				first = arr[j];
			} else if (arr[j] > second) {
				second = arr[j];
			}
			ans += second;
		}
	}
	cout << ans << endl;
	return 0;
}

 G.Num

Title Description

Phenix has a positive integer NNN. He wants to know whether the positive integer N can be written in the form of a * b+a+ba*b+a+ba * b+a+b (where a > 0, b > 0A > 0, b > 0A > 0, b > 0)
For example, if 3 = 1 * 1 + 1 + 13 = 1 * 1 + 1 + 13 = 1 * 1 + 1 + 1, please output "Yes" if possible, otherwise output "No"

Enter Description:

A positive integer n (0 < n < 2 \ times10 ^ 9) n (0 < n < 2 × 109)

Output Description:

"Yes" or "No"“

Example 1

input

2

output

No

Example 2

input

8

output

Yes

code:

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

int main() {
	int n;
	scanf("%d", &n);
	n++;
	if (n < 4) {
		puts("No");
		return 0;
	}
	int t = sqrt(n);
	for (int i = 2; i <= t; i ++)
		if (n % i == 0) {
			puts("Yes");
			return 0;
		}
	puts("No");
	return 0;
}

H. eigenvalues

Title Description

Recently, Jiebao learned the knowledge of linear algebra and successfully got 100 points in the final exam.

He was deeply impressed by the question of calculating the eigenvalues of the matrix.

Out of curiosity, he decided to use his vacation time to carefully study the concept of eigenvalue. After many days of closed door research, jabber put forward the concept of eigenvalue of integer.

Lovely jabber definition: for any positive integer X, its eigenvalue is calculated as follows: eigenvalue = ∑ k=010100 ⌊ X10k ⌋ \ sum_{k=0}^{10^{100}}\left\lfloor\frac{X}{10^{k}}\right\rfloor ∑ k=010100 ⌊ 10kX ⌋ note: ⌊ is rounded down, that is, it does not exceed the maximum integer of the current number (⌊ 3.2 ⌋ = 3, ⌊ 2.9 ⌋ = 2, ⌊ 7 ⌋ = 7)

Now jabber wants to popularize the concept. He needs your help to design a program that can quickly calculate the eigenvalue of a positive integer arbitrarily read in

Enter Description:

The input includes 1 line in total. Enter the number X that jabber wants to study
 Where 1 ≤ x < 10500000

Output Description:

The output includes one line, and the eigenvalue of the studied number is output

Example 1

input

1225

output

1360

explain

1225+122+12+1=1360

Example 2

input

99999

output

111105

explain

99999+9999+999+99+9=111105

Example 3

input

314159265358979323846264338327950288419716939937510

output

349065850398865915384738153697722542688574377708317

remarks
Tip: because the data read in in this question has a large data range, you can consider using an array of int type to store each bit of X for subsequent operations.
The same is true for calculating the answer and outputting the answer. You can use an array to store each bit of the number.

code:

#include <bits/stdc++.h>
using namespace std;
string x;
int s, c;
string res;

int main() {
	cin >> x;
	for (int i = 0; i < x.length(); i++)
		s += x[i] - '0';
	for (int i = x.length() - 1;; i--) {
		c += s;
		res.push_back('0' + (c % 10));
		c /= 10;
		if (i >= 0)
			s -= (x[i] - '0');
		if (i <= 0 && c == 0)
			break;
	}
	reverse(res.begin(), res.end());
	cout << res;
	return 0;
}

I. maximum common divisor

Title Description


Wenwen is addicted to learning mathematics and can't extricate herself. He has encountered problems in closed practice. Can you help him solve them?
Now give you a sequence PPP of NNN numbers, and define XXX as the maximum common factor of the NNN numbers. You can perform infinite operations, and select a group of (L,R)(L,R)(L,R) (1 ≤ L ≠ R ≤ N,PL ≥ 1) (1 \ Le L \ NEQ R \ Le n, p_l \ GE1) (1 ≤ L  = R ≤ N,PL ≥ 1) for each operation, so that PL − 1P_L-1PL − 1 and PR+1P_R+1PR + 1, each operation generates a new sequence PPP. Please tell me the maximum number of XXX corresponding to all possible PPPs. AC can obtain a signed photo of Wenwen with RP.
Definition: AAA is A common factor of sequence PPP if and only if each element of sequence PPP can be divided by A, and A ≥ 1A\ge1A ≥ 1.
Note: this question stipulates that any positive integer is a common factor of 0.

Enter Description:

The first row contains a number NNN, where 2 ≤ N ≤ 5002 \le N \le 5002 ≤ N ≤ 500.

The second row is a sequence with a length of NNN, in which the sequence element PPP satisfies
1≤P≤1061 \le P \le 10^{6}1≤P≤106. 

Output Description:

The output includes an integer representing the number of maximum common factors generated by all sequences.

Example 1

input

3
1 1 2

output

3

explain

For the first operation, L=1, R=2L=1, R=2L=1, R=2. After the operation, P=0,2,2P=0,2,2P=0,2,2. At this time, the maximum common factor of PPP is 222.

For the second operation, L=2, R=3L=2, R=3L=2, R=3. After the operation, P=0,1,3P=0,1,3P=0,1,3. At this time, the maximum common factor of PPP is 111.

For the third operation, L=2, R=3L=2, R=3L=2, R=3, P=0,0,4P=0,0,4P=0,0,4 after operation. At this time, the maximum common factor of PPP is 444.

After several operations, the maximum common factor generated by sequence PPP is 111222444, a total of 333.

code:

#include <bits/stdc++.h>
using namespace std;
int n, arr[510], s, sum;

int main() {

	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i], sum += arr[i];

	for (int i = 1; i * i <= sum; i++)
		if (sum % i == 0) {
			if (i * i == sum)
				s += 1;
			else
				s += 2;
		}
	cout << s;

	return 0;
}

J. string of Xuanshen

Title Description


Xuanshen has a magical string that has been treasured for many years. For some reason, the string consists only of the numbers 0 and 1. But recently, he began to get tired of strings, so he wanted to delete them. He can perform the following three operations at will, and the number of times of each operation is unlimited:
1) Select a 0 and a 1 in the string and delete them at the cost of a;
2) Select two zeros with different positions or two 1s with different positions in the string and delete them at the cost of b;
3) Change a 0 in the string to 1 or a 1 in the string to 0 at the cost of c.
Smart, can you help Xuanshen find out the minimum cost of deleting this magical string into an empty string (a string without any characters)?

Enter Description:

The first line includes a string s consisting only of the number 0 and the number 1. The length of s does not exceed 500005000050000. Ensure that the length of S is an even number
 The second line includes three integers a, b and c, representing the costs of the above three operations respectively. Guarantee 1 ≤ a,b,c ≤ 100001\leq a,b,c\leq 100001 ≤ a,b,c ≤ 10000

Output Description:

The output includes an integer representing the minimum cost of deleting the string

Example 1

input

0110011001
3 4 5

output

15

Example 2

input

0110011001
10 2 3

output

13

code:

#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, ans = 0, num[2];

int main() {
	string s;
	cin >> s;
	cin >> a >> b >> c;
	n = s.size();
	ans = 0;
	num[0] = num[1] = 0;
	for (int i = 0; i < n; ++i) {
		num[s[i] - '0']++;
	}
	if (a >= b + c) {
		ans = (num[0] & 1) * c + n / 2 * b;
	} else if (b >= a + c) {
		ans = (n / 2 - min(num[0], num[1])) * c + n / 2 * a;
	} else if (a >= b) {
		ans = num[0] / 2 * b + num[1] / 2 * b;
		if (num[0] & 1)
			ans += a;
	} else {
		ans = min(num[0], num[1]) * a + (max(num[0], num[1]) - min(num[0], num[1])) / 2 * b;
	}
	cout << ans << endl;
	return 0;
}

K. gold Chef

Title Description


As the gold medal chef in the canteen, Phenix prepares meals for the students every day. Every dish made by Phenix has a spicy value, ranging from [1,n][1,n][1,n]. As a chef, Phenix learned the spicy acceptance range of M students in advance. The spicy acceptance range of the I student is described as [li,ri][l_i,r_i][li, ri], indicating that the student can accept dishes with spicy value in this range. Because it is difficult for everyone to adjust, Phenix will select some students every day to make spicy dishes that can be accepted by these students. As a gold medal chef, Phenix's satisfaction with daily work is defined as the minimum of the number of selected students kkk and the number of dishes xxx (understood here as a spicy degree corresponding to a dish), that is, min (k, x) min (k, x) min (k, x) n, Mn, Mn, m (1 < = n, m < = 300000) (1 < = n, m < = 300000) (1 < = n, m < = 300000).
Now you need to find a way to maximize Phoenix's satisfaction.

Enter Description:

Two integers n,mn,mn,m in the first line represent the maximum spicy degree of the dish and the number of students (1 < = n, m < = 300000) (1 < = n, m < = 300000) (1 < = n, m < = 300000).
Next mmm lines, two integers Li, RIL per line_ i,r_ ILI, ri successively represent the spicy acceptance range of the third student (1 < = Li, ri < = n) (1 < = l_i, r_i < = n) (1 < = Li, ri < = n)

Output Description:

One line indicates the maximum satisfaction.

Example 1

input

5 5
3 5
1 2
2 5
2 5
4 5

output

3

explain
The optimal strategy is: select the first, third and fourth students, and their acceptance ranges of spicy degree are [3,5], [2,5] and [2,5], so the spicy degree of dishes that can be accepted by them is 3, 4 and 5. At this time, k=3,x=3, satisfaction = min(k,x)=3

code:

#include <cstdio>
#include <iostream>
#include <queue>
const int N = 3e5 + 10;
using namespace std;

struct ll {
	int r, last;
} d[N];
int n, m, ans, num, head[N];

int read() {
	int k = 0, f = 0;
	char c = getchar();
	for (; c < '0' || c > '9'; c = getchar())
		if (c == '-')
			f = 1;
	for (; c <= '9' && c >= '0'; c = getchar())
		k = (k << 3) + (k << 1) + c - '0';
	return f ? -k : k;
}

bool check(int x) {
	int cnt = 0;
	priority_queue<int>q;
	for (int i = 1; i + x - 1 <= n; i ++) {
		for (int t = head[i]; t; t = d[t].last)
			if (d[t].r >= i + x - 1)
				cnt++, q.push(-d[t].r);
		while (!q.empty() && -q.top() < i + x - 1)
			cnt--, q.pop();
		if (cnt >= x)
			return true;
	}
	return false;
}

void Dce(int l, int r) {
	while (l <= r) {
		int mid = l + r >> 1;
		if (check(mid))
			ans = mid, l = mid + 1;
		else
			r = mid - 1;
	}
	printf("%d\n", ans);
}

void add(int a, int b) {
	d[++num] = (ll) {
		b, head[a]
	};
	head[a] = num;
}

int main() {
	n = read();
	m = read();
	for (int i = 1; i <= m; i ++) {
		int a = read(), b = read();
		add(a, b);
	}
	Dce(0, min(n, m));
	return 0;
}

 L.WireConnection

Title Description

Gu Bao recently completed the construction of various heavy machinery and generators of immersive Engineering (IE), but the dense wires over the base made him feel very unsightly. He decided to remove all the wires with engineer's wire cutters and set up the grid with high-voltage wires again.

Every heavy machinery or generator has only one connector to connect wires. Gubao has only one goal, that is, to connect all connectors of the whole base to the same power grid, and the total length of wires required is the shortest. Since the wire can only be made into an integer length, if the distance between two connectors is not an integer, the required wire length needs to be * * rounded up * *.

In formal form, if the coordinates of the two connectors A and B are (XA, YA, YA, ZA, ZA), (XB, YB, YBB, ZB, XB, YB, ZB, XB, y_a, y_a, z_a, za, (x_b, y u b, z_b) (XA, YA, ZA, YA, YB, ZB, if the coordinates of the two connectors A and B are (XA, YA, YA, YA, YB, YB, and ZB, respectively) (XA, YA, YA, YB), if the coordinates of the two connectors A and B are respectively (XA, YA, YA, YA, YB, ZB), (XB, YA, YB, YB), then the distance between them is ⌈ (XA − − − − − XB XB) 2 + (XA − − XB)2+(YA −) XB) 2+(ZA −)) (XA − XB)2+(YA − YB)2+(ZA − ZB)2 ⌉, Where ⌈ \ lceil \rceil ⌈ ⌉ is rounded up, that is, it is not less than the minimum integer of the current number. For example, ⌈ 3 ⌉ = 3\lceil 3\rceil = 3 ⌈ 3 ⌉ = 3, ⌈ 3.14 ⌉ = 4\lceil 3.14\rceil=4 ⌈ 3.14 ⌉ = 4.

Enter Description:

The first line contains a positive integer n(n ≤ 2000)n(n\leq2000)n(n ≤ 2000), indicating the number of connectors.

The next nnn line contains three integers xxx, yyy and zzz (∣ x ∣, ∣ y ∣, ∣ z ∣ ≤ 109|x|,|y|,|z|\leq10^9 ∣ x ∣, ∣ y ∣, ∣ z ∣ ≤ 109) representing the spatial coordinates of the connector.

Output Description:

Minimum total length of wires required.

Example 1

input

5
0 0 0
1 0 0
-1 0 0
0 1 0
0 -1 0

output

4

remarks
The Ceil Function in the cmath header file of c + + can realize the upward rounding of floating-point numbers. For example, ceil(x) returns the result of X upward rounding.

code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10001;

struct node {
	long long x, y, z;
};

long long distCalc(node x, node y) {
	return ceil(sqrt((double)((x.x - y.x) * (x.x - y.x)) + ((x.y - y.y) * (x.y - y.y)) + ((x.z - y.z) * (x.z - y.z))));
}

int main() {
	int n;
	cin >> n;
	node p[MAXN];
	for (int i = 0; i < n; ++i) {
		cin >> p[i].x >> p[i].y >> p[i].z;
	}
	bool vis[MAXN];
	long long len[MAXN];
	memset(vis, 0, sizeof(vis));
	memset(len, 0x7f7f7f7f, sizeof(len));
	len[0] = 0;
	long long ans = 0;
	for (int i = 0; i < n; ++i) {
		long long minLen = INT_MAX;
		int newNode = 0;
		for (int j = 0; j < n; ++j) {
			if (!vis[j] && len[j] < minLen) {
				minLen = len[j];
				newNode = j;
			}
		}
		ans += minLen;
		vis[newNode] = 1;
		for (int j = 0; j < n; ++j) {
			len[j] = min(len[j], distCalc(p[j], p[newNode]));
		}
	}
	cout << ans << endl;
	return 0;
}

  M.NuclearReactor

Title Description

Gu Bao has been studying the industrial Craft 2 (IC2) nuclear reactor recently, but he is not skilled. He often melts the core and blows up the base. He found the attribute description of various components of the nuclear reactor on the Wiki and provided it to you. I hope you can help him make a nuclear reactor operation simulator to judge whether the nuclear reactor he designed can operate safely for a cycle.

The nuclear reactor setting interface of IC2 is a 6 × 96\times96 × 9 grid, each grid can place a component or empty. The nuclear reactor itself can absorb up to 10000 points of heat during operation. When the heat exceeds the limit, the explosion spectacle of core meltdown will occur.

In order to facilitate the collection of materials, Gubao's nuclear reactor plans to use only three components - uranium fuel rods, neutron reflectors and heat sinks.

The new uranium fuel rod can run for 200002000020000 seconds, that is, one cycle. Uranium fuel rods can be combined into dual fuel rods or quadruple fuel rods to save space. During operation, the uranium fuel rod will emit 111 neutrons to each of the four adjacent grids, and the number of neutrons emitted by the combined fuel rod increases to 222 or 444. At the same time, the heat of the fuel rod will be evenly distributed to the adjacent class A fins, and the heat that cannot be absorbed by class A fins will become the heat of the nuclear reactor itself.

Let the power generation of the fuel rod be WWW, the heat generation be QQQ, the number of neutrons received be nnn, the number of links be mmm, and the basic efficiency coefficient be η\ eta η, So there

{W=5m(η+n)Q=2m(η+n)(η+n+1)\begin{cases} W = 5m (\eta + n) \\ Q = 2m (\eta + n) (\eta + n + 1) \end{cases}{W=5m(η+n)Q=2m(η+n)(η+n+1)​

The values of mmm are 111, 222 or 444, and the corresponding basic efficiency coefficients are 111, 222 and 333.

The neutron reflector itself does not generate any electricity or heat, but it will reflect the received neutrons back to the fuel rod.

There are three categories of heat sinks, which have maximum heat absorption speed, maximum heat dissipation speed and heat buffer (except class C heat sink).

Class A heat sink will absorb heat from adjacent fuel rods, and its maximum heat absorption speed is equal to the maximum heat dissipation speed. The heat exchange rate of ordinary class A heat sink is 666 points per second, while that of advanced class A heat sink is 121212 points per second. Since the maximum heat absorption speed and the maximum heat dissipation speed are equal, when the class A heat sink is adjacent to more than one fuel rod, it may not be able to dissipate all the absorbed heat. When the heat exceeds 1000 points, it will melt itself. When it melts, the excess heat will not enter the heat buffer of the nuclear reactor.

Class B fins absorb heat from the reactor itself. Ordinary class B heat sink absorbs and dissipates up to 555 points of heat per second. The advanced class B heat sink absorbs up to 323232 points of heat per second and dissipates 202020 points of heat. When the heat exceeds 1000 points, it will melt itself. When it melts, the excess heat will not enter the heat buffer of the nuclear reactor. Nuclear reactor can intelligently control class B heat sink to ensure that the heat absorbed by the same class B heat sink is equal, and ordinary class B heat sink will be used preferentially, so as to maximize the use of class B heat sink. In addition, class B heat sink can only absorb the heat of the existing nuclear reactor. In other words, the heat entering the nuclear reactor heat buffer at one time can not be dissipated until at least the next time.

Class C heat sink is used to assist the other two types of heat sink, and can dissipate up to 444 points of heat per second from each adjacent other heat sink.

In order to simplify the problem, there is no startup problem in the nuclear reactor, that is, the fuel rod can receive neutrons from adjacent fuel rods and neutron reflection plates in the first second. In addition, the problem that the heat sink and nuclear reactor cannot receive the heat at a certain time due to heat overrun is not considered. In other words, the settlement of heat exchange precedes the meltdown.

Enter Description:

A grid with six rows and nine columns. Each grid contains one character to represent the components in the grid.
"#" represents empty, "U" represents uranium fuel rod, "D" represents duplex fuel rod, "Q" represents quadruple fuel rod, "N" represents neutron reflector, "a" represents advanced class a heat sink, "a" represents ordinary class a heat sink, "B" represents advanced class B heat sink, "B" represents ordinary class B heat sink, "C" represents class C heat sink.

Output Description:

1. Whether there will be a meltdown. If there will be a meltdown, output Boom! In the first line!, If it can operate safely for one cycle, the heat of the nuclear reactor at the end of operation shall be given (two decimal places shall be reserved).
2. Total power generation at the end of melting or operation.

Example 1

input

BBNNCaCaN
NDQBbUa#N
AQAQQNNU#
AUAQ#QAQN
QDNCNNUCB
QQNUbUaCa

output

Boom!
100020

Example 2

input

#########
#########
##ADA####
###a#####
#########
#########

output

Boom!
4550

Example 3

input

#########
###UaU###
#########
#########
#########
#########

output

Boom!
17520

code:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;
const int MAXX = 6 + 2;
const int MAXY = 9 + 2;
const int MAXTemp = 10000;
const int MAXTime = 20000;
const int MAXVentTemp = 1000;
const int dx[4] {0, -1, 0, 1};
const int dy[4] {-1, 0, 1, 0};
char map[MAXX][MAXY];
long long mapRodHeat[MAXX][MAXY];
double mapVentHeat[MAXX][MAXY];
long long mapVentANum[MAXX][MAXY];
long long ventBNum, ventbNum;
long long mapVentCNum[MAXX][MAXY];

int main() {
	memset(map, 0, sizeof(map));
	for (int i = 1; i < MAXX - 1; ++i) {
		for (int j = 1; j < MAXY - 1; ++j) {
			map[i][j] = getchar();
		}
		getchar();
	}
	long long answ = 0;
	memset(mapRodHeat, 0, sizeof(mapRodHeat));
	memset(mapVentANum, 0, sizeof(mapVentANum));
	memset(mapVentCNum, 0, sizeof(mapVentCNum));
	for (int i = 1; i < MAXX - 1; ++i) {
		for (int j = 1; j < MAXY - 1; ++j) {
			if (map[i][j] == 'U' || map[i][j] == 'D' || map[i][j] == 'Q') {
				int n = 0, m = 1, eta = 1;
				if (map[i][j] == 'D') {
					m = 2;
					eta = 2;
				} else if (map[i][j] == 'Q') {
					m = 4;
					eta = 3;
				}
				for (int di = 0; di < 4; ++di) {
					if (map[i + dx[di]][j + dy[di]] == 'U') {
						n += 1;
					} else if (map[i + dx[di]][j + dy[di]] == 'D') {
						n += 2;
					} else if (map[i + dx[di]][j + dy[di]] == 'Q') {
						n += 4;
					} else if (map[i + dx[di]][j + dy[di]] == 'N') {
						n += m;
					}
				}
				answ += 5 * m * (eta + n);
				mapRodHeat[i][j] = 2 * m * (eta + n) * (eta + n + 1);
			} else if (map[i][j] == 'A' || map[i][j] == 'a') {
				for (int di = 0; di < 4; ++di) {
					++mapVentANum[i + dx[di]][j + dy[di]];
				}
			} else if (map[i][j] == 'B') {
				++ventBNum;
			} else if (map[i][j] == 'b') {
				++ventbNum;
			} else if (map[i][j] == 'C') {
				for (int di = 0; di < 4; ++di) {
					++mapVentCNum[i + dx[di]][j + dy[di]];
				}
			}
		}
	}
	long long ansW = 0;
	double ansQ = 0;
	bool isBoom = 0;
	memset(mapVentHeat, 0, sizeof(mapVentHeat));
	for (int k = 0; k < MAXTime; ++k) {
		ansW += answ;
		ansQ = max((double)0, ansQ - (5 * ventbNum));
		double heatToVentB = ansQ;
		if (ventBNum != 0)
			ansQ = 0;
		for (int i = 1; i < MAXX - 1; ++i) {
			for (int j = 1; j < MAXY - 1; ++j) {
				if (mapRodHeat[i][j] != 0 && mapVentANum[i][j] == 0) {
					ansQ += mapRodHeat[i][j];
				} else if (mapVentHeat[i][j] > MAXVentTemp)
					continue;
				else if (map[i][j] == 'A') {
					for (int di = 0; di < 4; ++di) {
						if (mapRodHeat[i + dx[di]][j + dy[di]] != 0) {
							mapVentHeat[i][j] += min((double)12, (double)mapRodHeat[i + dx[di]][j + dy[di]] / mapVentANum[i + dx[di]][j + dy[di]]);
							ansQ += max((double)0, (double)mapRodHeat[i + dx[di]][j + dy[di]] / mapVentANum[i + dx[di]][j + dy[di]] - 12);
						}
					}
				} else if (map[i][j] == 'a') {
					for (int di = 0; di < 4; ++di) {
						if (mapRodHeat[i + dx[di]][j + dy[di]] != 0) {
							mapVentHeat[i][j] += min((double)6, (double)mapRodHeat[i + dx[di]][j + dy[di]] / mapVentANum[i + dx[di]][j + dy[di]]);
							ansQ += max((double)0, (double)mapRodHeat[i + dx[di]][j + dy[di]] / mapVentANum[i + dx[di]][j + dy[di]] - 6);
						}
					}
				} else if (map[i][j] == 'B') {
					mapVentHeat[i][j] += min((double)32, (double)heatToVentB / ventBNum);
					ansQ += max((double)0, (double)heatToVentB / ventBNum - 32);
				}
			}
		}
		for (int i = 1; i < MAXX - 1; ++i) {
			for (int j = 1; j < MAXY - 1; ++j) {
				if (mapVentHeat[i][j] > MAXVentTemp + 10000)
					continue;
				else if (map[i][j] == 'A') {
					mapVentHeat[i][j] = max((double)0, mapVentHeat[i][j] - 12 - 4 * mapVentCNum[i][j]);
					if (mapVentHeat[i][j] > MAXVentTemp) {
						mapVentHeat[i][j] += 10000;
						for (int di = 0; di < 4; ++di) {
							--mapVentANum[i + dx[di]][j + dy[di]];
						}
					}
				} else if (map[i][j] == 'a') {
					mapVentHeat[i][j] = max((double)0, mapVentHeat[i][j] - 6 - 4 * mapVentCNum[i][j]);
					if (mapVentHeat[i][j] > MAXVentTemp) {
						mapVentHeat[i][j] += 10000;
						for (int di = 0; di < 4; ++di) {
							--mapVentANum[i + dx[di]][j + dy[di]];
						}
					}
				} else if (map[i][j] == 'B') {
					mapVentHeat[i][j] = max((double)0, mapVentHeat[i][j] - 20 - 4 * mapVentCNum[i][j]);
					if (mapVentHeat[i][j] > MAXVentTemp) {
						mapVentHeat[i][j] += 10000;
						--ventBNum;
					}
				}
			}
		}
		if (ansQ > MAXTemp) {
			isBoom = 1;
			break;
		}
	}

	if (isBoom)
		cout << "Boom!";
	else
		printf("%.2lf", ansQ);
	cout << endl << ansW << endl;

	return 0;
}

Topics: C C++