Codeforces Round 920 (Div. 3) A ~ C 题解

目录

 

Problem A: Square

题目描述

Input

Output

Example

input

output

题目分析

题解

Problem B: Arranging Cats

题目描述

Input

Output

Example

input

output

题目分析

题解

 Problem C: Sending Messages

题目描述

Input

Output

Example

input

output

题目分析

题解


 

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≤ ≤100) — the number of testcases. The following is a description of the testcases.

Each testcase contains four lines, each line contains two integers eq?x_%7Bi%7D,eq?y_%7Bi%7D (−1000≤eq?x_%7Bi%7D,eq?y_%7Bi%7D≤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 eq?b_%7B1%7D,…,eq?b_%7Bn%7D: eq?b_%7Bi%7D%20%3D%201 if there is a cat in box number eq?i, and eq?b_%7Bi%7D%20%3D%200 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 eq?i such that eq?b_i%20%3D%200, assign eq?b_i%20%3D%201).
  • Remove a cat from a box and send it into retirement (for some eq?i such that eq?b_i%20%3D%201, assign eq?b_i%20%3D%200).
  • Move a cat from one box to another (for some eq?i%2C%20j such that eq?b_i%20%3D%201%2C%20b_j%20%3D%200, assign eq?b_i%20%3D%200%2C%20b_j%20%3D%201).

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 eq?s_1%2C...%2Cs_n and the desired position eq?f_1%2C...%2Cf_n.

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 eq?t eq?%281%5Cleq%20t%5Cleq%2010%5E4%29 — 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 eq?n eq?%281%20%5Cleq%20n%20%5Cleq%2010%5E5%20%29 — the number of boxes.

The second line of each test case contains a string eq?s of eq?n characters, where the eq?i-th character is '1' if there is a cat in the eq?i-th box and '0' otherwise.

The third line of each test case contains a string eq?f of eq?n characters, where the eq?i-th character is '1' if there should be a cat in the eq?i-th box and '0' otherwise.

It is guaranteed that in a test the sum of eq?n over all test cases does not exceed eq?10%5E5.

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 eq?n messages at moments eq?m_%7B1%7D%2C%20m_%7B2%7D%2C%20...%20%2Cm_n (eq?m_i%3Cm_i+1). Unfortunately, by the moment eq?0, his phone only has eq?f units of charge left. At the moment eq?0, the phone is turned on.

The phone loses eq?a 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 eq?b units of energy each time. Consider turning on and off to be instantaneous, so you can turn it on at moment eq?x and send a message at the same moment, and vice versa, send a message at moment eq?x and turn off the phone at the same moment.

If at any point the charge level drops to eq?0 (becomes eq?%5Cleq%200), 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 eq?t eq?%281%5Cleq%20t%5Cleq%2010%5E4%29 — 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 eq?neq?feq?a, and eq?b (eq?1%20%5Cleq%20n%20%5Cleq%202%20%5Ccdot%2010%5E%7B5%7Deq?1%5Cleq%20f%2Ca%2Cb%5Cleq%2010%5E9) — 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 eq?n integers eq?m_%7B1%7D%2C%20m_%7B2%7D%2C%20...%20%2Cm_n (eq?1%5Cleq%20m_i%5Cleq%2010%5E9eq?m_i%3Cm_%7Bi+1%7D) — the moments at which messages need to be sent.

It is guaranteed that in a test the sum of eq?n over all test cases does not exceed eq?2%20%5Ccdot%2010%5E5.

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 与开关机一次所消耗的电量 ,取 keep 和 的最小值,当前剩余电量 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")   

水平有限,敬请指出错误和提出宝贵意见建议。

 

  • 25
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值