Codeforces Round 966 (Div. 3) 前四题python题解

A. Primary Task

time limit per test

1 second

memory limit per test

256 megabytes

Dmitry wrote down t integers on the board, and that is good. He is sure that he lost an important integer n among them, and that is bad.

The integer nn had the form 10^x (x≥2), where the symbol '^' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '^^' when writing the important integer. For example, instead of the integer 10**5, he would have written 105105, and instead of 10**19, he would have written 10**19.

Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.

德米特里在黑板上写下了t 个整数,这很好。他肯定自己丢失了其中一个重要的整数 n ,这就不好了。

整数 n 的形式是 10^x ( x≥2 ),其中符号" ^ "表示指数化。出错了,德米特里在写重要整数时漏掉了符号" ^ "。例如,他应该写 10**5 而不是整数 10**5 ,应该写 10**19 而不是 10**19 。

德米特里想知道黑板上的整数哪些可能是重要整数,哪些不可能。

Input

The first line of the input contains one integer t (1≤t≤10**4) — the number of integers on the board.

The next tt lines each contain an integer a (1≤a≤10000) — the next integer from the board.

DeepL 翻译

输入

输入的第一行包含一个整数 tt ( 1≤t≤10**4 ) --棋盘上的整数个数。

接下来的 tt 行每行都包含一个整数 aa ( 1≤a≤10000) - 即棋盘上的下一个整数。

Output

For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise.

You may 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.

输出

对于棋盘上的每个整数,如果可能是重要整数,则输出 "是",否则输出 "否"。

您可以用任何大小写(小写或大写)输出每个字母。例如,字符串 "yEs"、"yes"、"Yes "和 "YES "将被视为肯定答案。

Example

Input

7

100

1010

101

105

2033

1019

1002

Output

NO
YES
NO
YES
NO
YES
NO
# 模拟判断就行题目给的很清楚
t = int(input())
for _ in range(t):
    s = input()
    if len(s) <= 2:
        print("NO")
        continue
    elif int(s[:2]) == 10 and int(s[2:]) < 2:
        print("NO")
        continue
    elif int(s[:2]) == 10 and len(s[2:]) != len(str(int(s[2:]))):
        print("NO")
        continue
    elif int(s[:2]) != 10:
        print("NO")
        continue
    else:
        print("YES")

 

B. Seating in a Bus

time limit per test

2 seconds

memory limit per test

256 megabytes

In Berland, a bus consists of a row of n seats numbered from 11 to n. Passengers are advised to always board the bus following these rules:

  • If there are no occupied seats in the bus, a passenger can sit in any free seat;
  • Otherwise, a passenger should sit in any free seat that has at least one occupied neighboring seat. In other words, a passenger can sit in a seat with index ii (1≤i≤n) only if at least one of the seats with indices i−1 or i+1 is occupied.

Today, n passengers boarded the bus. The array a chronologically records the seat numbers they occupied. That is, a1 contains the seat number where the first passenger sat, a2 — the seat number where the second passenger sat, and so on.

You know the contents of the array aa. Determine whether all passengers followed the recommendations.

For example, if n=5, and a = [5,4,2,1,3], then the recommendations were not followed, as the 3-rd passenger sat in seat number 2, while the neighboring seats with numbers 1 and 3 were free.

在伯兰,一辆公共汽车由一排从 1 到n 的 n 个座位组成。乘客上车时请务必遵守这些规则:

  • 如果车上没有空座位,乘客可以坐在任何空座位上;
  • 否则,乘客应坐在至少有一个邻座空闲的座位上。换句话说,只有当索引为 i−1 或 i+1 的座位中至少有一个有人时,乘客才能坐在索引为 i ( 1≤i≤n )的座位上。

今天有 n 位乘客上车。数组 a 按时间顺序记录了他们的座位号。也就是说, a1 包含了第一位乘客的座位号,a2 --第二位乘客的座位号,以此类推。

您知道数组 a 的内容。确定是否所有乘客都遵循了建议。

例如,如果 n=5 和 a =[ 5,4,2,1,3 ],那么建议就没有被遵守,因为 3 -rd乘客坐在了座位号为 2 的座位上,而相邻的座位号为 1 和 3 的座位是空闲的。

Input

The first line of input contains a single integer t (1≤t≤10**4) — the number of test cases.

The following describes the input test cases.

The first line of each test case contains exactly one integer n (1≤n≤2⋅10**5) — the number of seats in the bus and the number of passengers who boarded the bus.

The second line of each test case contains nn distinct integers ai (1≤ai≤n) — the seats that the passengers occupied in chronological order.

It is guaranteed that the sum of nn values across all test cases does not exceed 2⋅10**5, and that no passenger sits in an already occupied seat.

输入

第一行输入包含一个整数 tt ( 1≤t≤10**4 ) - 测试用例数。

下面介绍输入的测试用例。

每个测试用例的第一行都包含一个整数 nn ( 1≤n≤2⋅10**5 ) - 公交车的座位数和上车乘客数。

每个测试用例的第二行都包含 n 个整数。不同的整数 ai ( 1≤ai≤n ) - 按时间顺序排列的乘客座位。

保证所有测试用例的 n 值总和不超过 2⋅10**5 ,并且没有乘客坐在已被占用的座位上。

Output

For each test case, output on a separate line:

  • "YES", if all passengers followed the recommendations;
  • "NO" otherwise.

You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

输出

每个测试用例的输出都单独成行:

  • 如果所有乘客都遵循了建议,则输出 "YES";
  • 否则输出 "否"。

您可以在任何情况下输出答案(例如,字符串 "yEs"、"yes"、"Yes "和 "YES "将被识别为肯定答案)。

Example

Input

4

5

5 4 2 1 3

3

2 3 1

4

2 3 1 4

5

1 2 3 5 4

Output

NO
YES
YES
NO
# 模拟
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    l = [""] * (n + 10)
    l[a[0]] = 1
    for i in range(1,n):
        if l[a[i] - 1] == "" and l[a[i] + 1] == "":
            print("NO")
            break
        l[a[i]] = 1
    else:
        print("YES")

C. Numeric String Template

time limit per test

2 seconds

memory limit per test

256 megabytes

Kristina has an array a, called a template, consisting of n integers. She also has m strings, each consisting only of lowercase Latin letters. The strings are numbered from 1 to m. She wants to check which strings match the template.

A string s is considered to match the template if all of the following conditions are simultaneously satisfied:

  • The length of the string s is equal to the number of elements in the array a.
  • The same numbers from a correspond to the same symbols from s. So, if ai=aj, then si=sj for (1≤i,j≤n).
  • The same symbols from s correspond to the same numbers from aa. So, if si=sj, then ai=aj for (1≤i,j≤n).

In other words, there must be a one-to-one correspondence between the characters of the string and the elements of the array.

For example, if aa = [3,5,2,1,,3], then the string "abfda" matches the template, while the string "afbfa" does not, since the character "f" corresponds to both numbers 1 and 5.

克里斯蒂娜有一个由 n 个整数组成的数组 a ,称为模板。她还有 m 个字符串,每个字符串都只由小写拉丁字母组成。这些字符串的编号从 1 到 m 。她想检查哪些字符串与模板匹配。

如果同时满足以下所有条件,则认为字符串 s 与模板匹配:

  • 字符串 s 的长度等于数组 a 中的元素个数。
  • a 中的相同数字对应于 s 中的相同符号。因此,如果 ai=aj ,那么 si=sj 为( 1≤i,j≤n )。
  • 来自 s 的相同符号对应于来自 aa 的相同数字。因此,如果是 si=sj,那么 ai=aj 就是( 1≤i,j≤n)。

换句话说,字符串中的字符与数组中的元素必须一一对应。

例如,如果 aa = [ 3,5,2,1,3 ],那么字符串 "abfda "与模板匹配,而字符串 "afbfa "则不匹配,因为字符 "f "同时对应数字 1 和 5 。

Input

The first line of input contains a single integer tt (1≤t≤10**4) — the number of test cases.

The following descriptions are for the test cases.

The first line of each test case contains a single integer nn (1≤n≤2⋅10**5) — the number of elements in the array a.

The second line of each test case contains exactly nn integers aiai (−10**9≤ai≤10**9) — the elements of the array a.

The third line of each test case contains a single integer mm (1≤m≤2⋅10**5) — the number of strings to check for template matching.

Following are m strings, each containing a non-empty string sjsj (1≤|sj|≤2⋅10**5), consisting of lowercase Latin letters.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅10**5, and that the sum of the lengths of all strings does not exceed 2⋅10**5.

输入

第一行输入包含一个整数 tt ( 1≤t≤10**4) - 测试用例数。

下面是测试用例的说明。

每个测试用例的第一行包含一个整数 n ( 1≤n≤2⋅10**5) - 数组 a 中的元素个数。

每个测试用例的第二行包含 n 个整数 ai ( −10**9≤ai≤10**9 ) - 数组 a 中的元素。

每个测试用例的第三行包含一个整数 m ( 1≤m≤2⋅10**5 ) - 要检查模板匹配的字符串数量。

下面是 m 个字符串,每个字符串都包含一个非空字符串 sjsj ( 1≤|sj|≤2⋅10**5 ),由小写拉丁字母组成。

保证所有测试用例中 nn 的总和不超过 2⋅10**5 ,所有字符串的长度总和不超过 2⋅10**5 。

Output

For each test case, output m lines. On the i-th line (1≤i≤m) output:

  • "YES", if the string with index i matches the template;
  • "NO" otherwise.

You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

DeepL 翻译

输出

对于每个测试用例,输出 m 行。在 i -th 行( 1≤i≤m )输出:

  • 如果索引为 ii 的字符串与模板匹配,则输出 "YES";
  • 否则输出 "NO"。

您可以在任何情况下输出答案(例如,字符串 "yEs"、"yes"、"Yes "和 "YES "将被识别为肯定答案)。

Example

Input

3

5

3 5 2 1 3

2

abfda

afbfa

2

1 2

3

ab

abc

aa

4

5 -3 5 -3

4

aaaa

bcbc

aba

cbcb

Output

YES
NO
YES
NO
NO
NO
YES
NO
YES
# 模拟
from collections import defaultdict
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    m = int(input())
    for _ in range(m):
        dict1 = defaultdict(int)
        s = input()
        if len(s) != n:
            print("NO")
            continue
        for i in range(n):
            if dict1[a[i]] == 0 and dict1[s[i]] not in dict1:
                dict1[a[i]] = s[i]
                dict1[s[i]] = a[i]
            elif dict1[a[i]] != s[i] or dict1[s[i]] != a[i]:
                print("NO")
                break
        else:
            print("YES")

 

D. Right Left Wrong

time limit per test

2 seconds

memory limit per test

256 megabytes

Vlad found a strip of n cells, numbered from left to right from 1 to n. In the i-th cell, there is a positive integer ai and a letter si, where all si are either 'L' or 'R'.

Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.

In one operation, you can choose two indices l and r (1≤l

  • add al+al+1+⋯+ar−1+ar points to the current score;
  • replace si with '.' for all l≤i≤r, meaning you can no longer choose these indices.

For example, consider the following strip:

3 5 1 4 3 2
L R L L L R

You can first choose l=1, r=2 and add 3+5=8 to your score.

3 5 1 4 3 2
. . L L L R

Then choose l=3, r=6 and add 1+4+3+2=10 to your score.

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

As a result, it is impossible to perform another operation, and the final score is 18.

What is the maximum score that can be achieved?

弗拉德发现了一个由 n 个单元格组成的长条,从左到右的编号为 1 到 n 。在第 i 个单元格中,有一个正整数 ai 和一个字母 si ,其中所有的 si 不是 "L "就是 "R"。

弗拉德邀请您尝试进行任意数量(可能为零)的运算,以获得尽可能多的分数。

在一次操作中,您可以选择两个索引 l 和 r ( 1≤l

  • 将al+1+⋯+ar−1+ar分数添加到当前分数中;
  • 将所有 l≤i≤r 的 si 替换为'.',这意味着您不能再选择这些索引。

例如,请考虑以下条带:

3 5 1 4 3 2
l r l l r

你可以先选择 l=1 、 r=2 ,然后在分数上加上 3+5=8 。

3 5 1 4 3 2
. .

然后选择 l=3 、 r=6 ,并将 1+4+3+2=10 加入你的分数。

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

因此,不可能再进行其他操作,最终得分是 18 。

最大得分是多少?

Input

The first line contains one integer tt (1≤t≤10**4) — the number of test cases.

The first line of each test case contains one integer nn (2≤n≤2⋅10**5) — the length of the strip.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤10**5) — the numbers written on the strip.

The third line of each test case contains a string s of n characters 'L' and 'R'.

It is guaranteed that the sum of the values of n across all test cases does not exceed 2⋅10**5.

输入

第一行包含一个整数 tt ( 1≤t≤10**4 )。( 1≤t≤10**4 ) - 测试用例的数量。

每个测试用例的第一行包含一个整数 nn ( 2≤n≤2⋅10**5 ) --条带长度。

每个测试用例的第二行包含 n 个整数 a1,a2,…,an ( 1≤ai≤10**5 ) - 写在条形图上的数字。

每个测试用例的第三行包含由 n 个字符 "L "和 "R "组成的字符串 s 。

保证所有测试用例中 n 的值之和不超过 2⋅10**5 。

Output

For each test case, output one integer — the maximum possible number of points that can be scored.

输出

对于每个测试用例,输出一个整数--可能得到的最大分数。

Example

Input

4

6

3 5 1 4 3 2

LRLLLR

2

2 8

LR

2

3 9

RL

5

1 2 3 4 5

LRLRR

Output

18
10
0
22
# 双指针 + 前缀和
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    s = input()
    a = [0] + a
    for i in range(1,n + 1):
        a[i] = a[i - 1] + a[i]
    i = 0; j = n
    ans = 0
    while i < j - 1:
        if s[i] == "L" and s[j - 1] == "R":
            ans += a[j] - a[i]
            j -= 1
            i += 1
        elif s[i] == "L" and s[j - 1] != "R":
            while i < j:
                if s[j - 1] == "R":
                    ans += a[j] - a[i]
                    break
                else:
                    j -= 1
            i += 1
            j -= 1
        elif s[i] != "L" and s[j - 1] == "R":
            while i < j:
                if s[i] == "L":
                    ans += a[j] - a[i]
                    break
                else:
                    i += 1
            j -= 1
            i += 1
        elif s[i] != "L" and s[j - 1] != "R":
            i += 1
            j -= 1
    print(ans)

你可能感兴趣的:(python,开发语言)