目录
Problem A: Square
题目描述
A square of positive (strictly greater than 0) area is located on the coordinate plane, with sides parallel to the coordinate axes. You are given the coordinates of its corners, in random order. Your task is to find the area of the square.
Input
Each test consists of several testcases. The first line contains one integer t (1≤ t ≤100) — the number of testcases. The following is a description of the testcases.
Each testcase contains four lines, each line contains two integers , (−1000≤,≤1000), coordinates of the corners of the square.
It is guaranteed that there is a square with sides parallel to the coordinate axes, with positive (strictly greater than 0) area, with corners in given points.
Output
For each test case, print a single integer, the area of the square.
Example
input
3
1 2
4 5
1 5
4 2
-1 1
1 -1
1 1
-1 -1
45 11
45 39
17 11
17 39
output
9
4
784
题目分析
已知正方形四角坐标求正方形面积,不需要任何方法,直接写。
题解
t = int(input())
for _ in range(t):
x = 0
y = 0
judge = 1 # 结果由x得到为1,由y得到为0
for i in range(4):
tmp_x, tmp_y = map(int, input().split())
if i > 0:
if tmp_x != x:
x -= tmp_x # 无需做绝对值处理
break # 找到一个不同即为边长
elif tmp_y != y:
y -= tmp_y
judge = 0
break
else:
x, y = tmp_x, tmp_y
print(x ** 2 * judge + y ** 2 * abs(judge - 1))
Problem B: Arranging Cats
题目描述
In order to test the hypothesis about the cats, the scientists must arrange the cats in the boxes in a specific way. Of course, they would like to test the hypothesis and publish a sensational article as quickly as possible, because they are too engrossed in the next hypothesis about the phone's battery charge.
Scientists have n boxes in which cats may or may not sit. Let the current state of the boxes be denoted by the sequence ,…,: if there is a cat in box number , and otherwise.
Fortunately, the unlimited production of cats has already been established, so in one day, the scientists can perform one of the following operations:
- Take a new cat and place it in a box (for some such that , assign ).
- Remove a cat from a box and send it into retirement (for some such that , assign ).
- Move a cat from one box to another (for some such that , assign ).
It has also been found that some boxes were immediately filled with cats. Therefore, the scientists know the initial position of the cats in the boxes and the desired position .
Due to the large amount of paperwork, the scientists do not have time to solve this problem. Help them for the sake of science and indicate the minimum number of days required to test the hypothesis.
Input
Each test consists of several test cases. The first line contains a single integer — the number of test cases. This is followed by descriptions of the test cases.
Each test case consists of three lines.
The first line of each test case contains a single integer — the number of boxes.
The second line of each test case contains a string of characters, where the character is '1' if there is a cat in the box and '0' otherwise.
The third line of each test case contains a string of characters, where the character is '1' if there should be a cat in the box and '0' otherwise.
It is guaranteed that in a test the sum of over all test cases does not exceed .
Output
For each test case, output a single integer on a separate line — the minimum number of operations required to obtain the desired position from the initial position. It can be shown that a solution always exists.
Example
input
6
5
10010
00001
1
1
1
3
000
111
4
0101
1010
3
100
101
8
10011001
11111110
output
2
0
3
2
1
4
题目分析
- 先算出两个数组中数字1的个数的差值su,每一个差值代表着需要执行一次操作1或操作2;
- 再按相同索引自0至n同时遍历两个数组,找出两个数组中数字1位置不同的个数 re ,每个 re 代表需要执行一次操作3;
(例:100和001,i = 0 时,两个数组的值分别为1和0,i = 2 时,两个数组的值分别为0和1,出现两次不同,但实际上只有一对数字1的位置不对应,最终 re 计1,即每次计0.5)
(注:当两个数组的数字1的个数不相等时,会出现多计,例如100和011,两个数组数字1的个数分别为1和2,此时 su 为2 - 1 = 1,在按此种方法计数时,re 会计出结果1.5,此时 re 减去 su * 0.5,最后 re 结果即为两个数组中数字1位置不同的个数) - su 代表执行一次操作1或2,而 re 代表执行一次操作3,则结果需要执行的操作次数 result 为 re 与 su 之和。
题解
t = int(input())
for i in range(t):
n = int(input())
s = [int(_) for _ in input()]
f = [int(_) for _ in input()]
ss = sum(s)
sf = sum(f)
su = abs(ss - sf)
re = - su * 0.5
for x in range(n):
if s[x] != f[x]:
re += 0.5
result = int(su + re)
print(result)
Problem C: Sending Messages
题目描述
Stepan is a very busy person. Today he needs to send messages at moments (). Unfortunately, by the moment , his phone only has units of charge left. At the moment , the phone is turned on.
The phone loses units of charge for each unit of time it is on. Also, at any moment, Stepan can turn off the phone and turn it on later. This action consumes units of energy each time. Consider turning on and off to be instantaneous, so you can turn it on at moment and send a message at the same moment, and vice versa, send a message at moment and turn off the phone at the same moment.
If at any point the charge level drops to (becomes ), it is impossible to send a message at that moment.
Since all messages are very important to Stepan, he wants to know if he can send all the messages without the possibility of charging the phone.
Input
Each test consists of several test cases. The first line contains a single integer — the number of test cases. This is followed by descriptions of the test cases.
Each test case consists of three lines.
The first line of each test case contains four integers , , , and (, ) — the number of messages, the initial phone's charge, the charge consumption per unit of time, and the consumption when turned off and on sequentially.
The second line of each test case contains integers (, ) — the moments at which messages need to be sent.
It is guaranteed that in a test the sum of over all test cases does not exceed .
Output
For each test case, output "YES" if Stepan can send all the messages, and "NO" otherwise.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
Example
input
6
1 3 1 5
3
7 21 1 3
4 6 10 13 17 20 26
5 10 1 2
1 2 3 4 5
1 1000000000 1000000000 1000000000
1000000000
3 11 9 6
6 8 10
12 621526648 2585904 3566299
51789 61859 71998 73401 247675 298086 606959 663464 735972 806043 806459 919683
output
NO
YES
YES
NO
NO
YES
题目分析
此题依次比较当前时刻 start 和下一需要发送消息的时刻 i (i 是每一个需要发送消息的时刻,是 m 中的元素)的之间一直保持开机状态所消耗的电量 keep 与开关机一次所消耗的电量 b ,取 keep 和 b 的最小值,当前剩余电量 result 减去该值,判断 result 是否小于等于零,若是,代表当前电量不足,当前消息无法发送,所以此时输出“NO”,当全部消息发送完成后 result 仍大于零,则表示完成,输出“YES”。(中间用 judge 作为记录 result 状态的变量,默认为 1)
题解
t = int(input())
for _ in range(t):
n, f, a, b = map(int, input().split())
m = list(map(int, input().split()))
start = 0
judge = 1
result = f
for i in m:
keep = (i - start) * a
result -= min(keep, b)
start = i
if result <= 0:
judge = 0
break
print("YES" if judge else "NO")
水平有限,敬请指出错误和提出宝贵意见建议。