Codeforces Round 806 (Div. 4)


A. YES or YES?

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

There is a string s s s of length 3 3 3, consisting of uppercase and lowercase English letters. Check if it is equal to “YES” (without quotes), where each letter can be in any case. For example, “yES”, “Yes”, “yes” are all allowable.

Input

The first line of the input contains an integer t t t ( 1 ≤ t ≤ 1 0 3 1 \leq t \leq 10^3 1t103) — the number of testcases.

The description of each test consists of one line containing one string s s s consisting of three characters. Each character of s s s is either an uppercase or lowercase English letter.

Output

For each test case, output “YES” (without quotes) if s s s satisfies the condition, and “NO” (without quotes) otherwise.

You can output “YES” and “NO” in any case (for example, strings “yES”, “yes” and “Yes” will be recognized as a positive response).

Example
input

10
YES
yES
yes
Yes
YeS
Noo
orZ
yEz
Yas
XES

output

YES
YES
YES
YES
YES
NO
NO
NO
NO
NO

Note

The first five test cases contain the strings “YES”, “yES”, “yes”, “Yes”, “YeS”. All of these are equal to “YES”, where each character is either uppercase or lowercase.

Tutorial
将字符串转换为小写后与 yes 做比较

Solution

for _ in range(int(input())):
    s = input().strip()
    print("YES" if s.lower() == "yes" else "NO")

B. ICPC Balloons

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output


In an ICPC contest, balloons are distributed as follows:

  • Whenever a team solves a problem, that team gets a balloon.
  • The first team to solve a problem gets an additional balloon.

A contest has 26 problems, labelled A \textsf{A} A, B \textsf{B} B, C \textsf{C} C, …, Z \textsf{Z} Z. You are given the order of solved problems in the contest, denoted as a string s s s, where the i i i-th character indicates that the problem s i s_i si has been solved by some team. No team will solve the same problem twice.

Determine the total number of balloons that the teams received. Note that some problems may be solved by none of the teams.

Input

The first line of the input contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of testcases.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 50 1 \leq n \leq 50 1n50) — the length of the string.

The second line of each test case contains a string s s s of length n n n consisting of uppercase English letters, denoting the order of solved problems.

Output

For each test case, output a single integer — the total number of balloons that the teams received.

Example
input

6
3
ABA
1
A
3
ORZ
5
BAAAA
4
BKPT
10
CODEFORCES

output

5
2
6
7
8
17

Note

In the first test case, 5 5 5 balloons are given out:

  • Problem A \textsf{A} A is solved. That team receives 2 2 2 balloons: one because they solved the problem, an an additional one because they are the first team to solve problem A \textsf{A} A.
  • Problem B \textsf{B} B is solved. That team receives 2 2 2 balloons: one because they solved the problem, an an additional one because they are the first team to solve problem B \textsf{B} B.
  • Problem A \textsf{A} A is solved. That team receives only 1 1 1 balloon, because they solved the problem. Note that they don’t get an additional balloon because they are not the first team to solve problem A \textsf{A} A.

The total number of balloons given out is 2 + 2 + 1 = 5 2+2+1=5 2+2+1=5.

In the second test case, there is only one problem solved. The team who solved it receives 2 2 2 balloons: one because they solved the problem, an an additional one because they are the first team to solve problem A \textsf{A} A.

Tutorial
当这个字母第一次出现时 + 2 +2 +2,以后出现都是 + 1 +1 +1

Solution

from collections import *

for _ in range(int(input())):
    input()
    ans = 0
    s = input().strip()
    mp = Counter()
    for c in s:
        ans += (1 if mp[c] else 2)
        if not mp[c]:
            mp[c] = True
    print(ans)

C. Cypher

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Luca has a cypher made up of a sequence of n n n wheels, each with a digit a i a_i ai written on it. On the i i i-th wheel, he made b i b_i bi moves. Each move is one of two types:

  • up move (denoted by U \texttt{U} U): it increases the i i i-th digit by 1 1 1. After applying the up move on 9 9 9, it becomes 0 0 0.
  • down move (denoted by D \texttt{D} D): it decreases the i i i-th digit by 1 1 1. After applying the down move on 0 0 0, it becomes 9 9 9.

Example for n = 4 n=4 n=4. The current sequence is 0 0 0 0.

Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \leq n \leq 100 1n100) — the number of wheels.

The second line contains n n n integers a i a_i ai ( 0 ≤ a i ≤ 9 0 \leq a_i \leq 9 0ai9) — the digit shown on the i i i-th wheel after all moves have been performed.

Then n n n lines follow, the i i i-th of which contains the integer b i b_i bi ( 1 ≤ b i ≤ 10 1 \leq b_i \leq 10 1bi10) and b i b_i bi characters that are either U \texttt{U} U or D \texttt{D} D — the number of moves performed on the i i i-th wheel, and the moves performed. U \texttt{U} U and D \texttt{D} D represent an up move and a down move respectively.

Output

For each test case, output n n n space-separated digits — the initial sequence of the cypher.

Example
input

3
3
9 3 1
3 DDD
4 UDUU
2 DU
2
0 9
9 DDDDDDDDD
9 UUUUUUUUU
5
0 5 9 8 3
10 UUUUUUUUUU
3 UUD
8 UUDUUDDD
10 UUDUUDUDDU
4 UUUU

output

2 1 1 
9 0 
0 4 9 6 9 

Note

In the first test case, we can prove that initial sequence was [ 2 , 1 , 1 ] [2,1,1] [2,1,1]. In that case, the following moves were performed:

  • On the first wheel: 2 → D 1 → D 0 → D 9 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{D}]{} 9 2 D1 D0 D9.
  • On the second wheel: 1 → U 2 → D 1 → U 2 → U 3 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{U}]{} 3 1 U2 D1 U2 U3.
  • On the third wheel: 1 → D 0 → U 1 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{U}]{} 1 1 D0 U1.

The final sequence was [ 9 , 3 , 1 ] [9,3,1] [9,3,1], which matches the input.

Tutorial
此题就是知结果推初始状态,根据题意反向模拟即可

Solution

for _ in range(int(input())):
    n = int(input())
    ans = list(map(int, input().split()))
    for i in range(n):
        s = list(map(str, input().split()))
        print((ans[i] + len(s[1]) - 2 * s[1].count('U')) % 10, end=" \n"[i == n - 1])

D. Double Strings

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You are given n n n strings s 1 , s 2 , … , s n s_1, s_2, \dots, s_n s1,s2,,sn of length at most 8 \mathbf{8} 8.

For each string s i s_i si, determine if there exist two strings s j s_j sj and s k s_k sk such that s i = s j + s k s_i = s_j + s_k si=sj+sk. That is, s i s_i si is the concatenation of s j s_j sj and s k s_k sk. Note that j j j can be equal to k k k.

Recall that the concatenation of strings s s s and t t t is s + t = s 1 s 2 … s p t 1 t 2 … t q s + t = s_1 s_2 \dots s_p t_1 t_2 \dots t_q s+t=s1s2spt1t2tq, where p p p and q q q are the lengths of strings s s s and t t t respectively. For example, concatenation of “code” and “forces” is “codeforces”.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105) — the number of strings.

Then n n n lines follow, the i i i-th of which contains non-empty string s i s_i si of length at most 8 \mathbf{8} 8, consisting of lowercase English letters. Among the given n n n strings, there may be equal (duplicates).

The sum of n n n over all test cases doesn’t exceed 1 0 5 10^5 105.

Output

For each test case, output a binary string of length n n n. The i i i-th bit should be 1 \texttt{1} 1 if there exist two strings s j s_j sj and s k s_k sk where s i = s j + s k s_i = s_j + s_k si=sj+sk, and 0 \texttt{0} 0 otherwise. Note that j j j can be equal to k k k.

Example
input

3
5
abab
ab
abc
abacb
c
3
x
xx
xxx
8
codeforc
es
codes
cod
forc
forces
e
code

output

10100
011
10100101

Note

In the first test case, we have the following:

  • s 1 = s 2 + s 2 s_1 = s_2 + s_2 s1=s2+s2, since abab = ab + ab \texttt{abab} = \texttt{ab} + \texttt{ab} abab=ab+ab. Remember that j j j can be equal to k k k.
  • s 2 s_2 s2 is not the concatenation of any two strings in the list.
  • s 3 = s 2 + s 5 s_3 = s_2 + s_5 s3=s2+s5, since abc = ab + c \texttt{abc} = \texttt{ab} + \texttt{c} abc=ab+c.
  • s 4 s_4 s4 is not the concatenation of any two strings in the list.
  • s 5 s_5 s5 is not the concatenation of any two strings in the list.

Since only s 1 s_1 s1 and s 3 s_3 s3 satisfy the conditions, only the first and third bits in the answer should be 1 \texttt{1} 1, so the answer is 10100 \texttt{10100} 10100.

Tutorial
由题意得每个 t e s t test test 的字符串个数范围为 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105,每个字符串的长度为多为 8 8 8,所以这就是一个暴力比较的过程,只需要先记忆每个字符串是否出现过,然后将每个字符串拆分,判断两个部分是否出现过即可

Solution

from collections import Counter

for _ in range(int(input())):
    n = int(input())
    mp = Counter()
    a = []
    for __ in range(n):
        s = input()
        a.append(s)
        mp[s] = True
    for s in a:
        flag = False
        for i in range(min(len(s), 8) - 1):
            if mp[s[:i + 1]] and mp[s[i + 1:]]:
                flag = True
                break
        print(1 if flag else 0, end="")
    print()

E. Mirror Grid

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You are given a square grid with n n n rows and n n n columns. Each cell contains either 0 0 0 or 1 1 1.

In an operation, you can select a cell of the grid and flip it (from 0 → 1 0 \to 1 01 or 1 → 0 1 \to 0 10). Find the minimum number of operations you need to obtain a square that remains the same when rotated 0 ∘ 0^{\circ} 0, 9 0 ∘ 90^{\circ} 90, 18 0 ∘ 180^{\circ} 180 and 27 0 ∘ 270^{\circ} 270.

The picture below shows an example of all rotations of a grid.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \leq n \leq 100 1n100) — the size of the grid.

Then n n n lines follow, each with n n n characters a i , j a_{i,j} ai,j ( 0 ≤ a i , j ≤ 1 0 \leq a_{i,j} \leq 1 0ai,j1) — the number written in each cell.

Output

For each test case output a single integer — the minimum number of operations needed to make the square look the same rotated 0 ∘ 0^{\circ} 0, 9 0 ∘ 90^{\circ} 90, 18 0 ∘ 180^{\circ} 180 and 27 0 ∘ 270^{\circ} 270.

Example
input

5
3
010
110
010
1
0
5
11100
11011
01011
10011
11000
5
01000
10101
01010
00010
01001
5
11001
00000
11111
10110
01111

output

1
0
9
7
6

Note

In the first test case, we can perform one operations to make the grid 0 1 0 1 1 1 0 1 0 \begin{matrix}0 & 1 & 0\\ 1 & 1 & \color{red}{1}\\ 0 & 1 & 0\end{matrix} 010111010. Now, all rotations of the square are the same.

In the second test case, all rotations of the square are already the same, so we don’t need any flips.

Tutorial
根据题意得,需要对 0 ∘ 0^{\circ} 0, 9 0 ∘ 90^{\circ} 90, 18 0 ∘ 180^{\circ} 180, 27 0 ∘ 270^{\circ} 270 四个旋转方向进行判断,所以需要将原本的正方形分成四个方块,然后以其中一个方块为基础,判断四个方块上对应的位置的值是什么,然后更改出现少的值就可以了(因为只会出现 0 0 0 1 1 1 两种值)

注意边界处理,避免漏判断以及重复判断

Solution

import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n = int(input())
    ans = 0
    g = [list(map(int, input().strip())) for __ in range(n)]
    for i in range(n // 2):
        for j in range((n + 1) // 2):
            cnt = g[i][j] + g[j][n - i - 1] + g[n - i - 1][n - j - 1] + g[n - j - 1][i]
            ans += min(cnt, 4 - cnt)
    print(ans)

F. Yet Another Problem About Pairs Satisfying an Inequality

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You are given an array a 1 , a 2 , … a n a_1, a_2, \dots a_n a1,a2,an. Count the number of pairs of indices 1 ≤ i , j ≤ n 1 \leq i, j \leq n 1i,jn such that a i < i < a j < j a_i < i < a_j < j ai<i<aj<j.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

The first line of each test case contains an integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \leq n \leq 2 \cdot 10^5 2n2105) — the length of the array.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 0 ≤ a i ≤ 1 0 9 0 \leq a_i \leq 10^9 0ai109) — the elements of the array.

It is guaranteed that the sum of n n n across all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the number of pairs of indices satisfying the condition in the statement.

Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Example
input

5
8
1 1 2 3 8 2 1 4
2
1 2
10
0 2 1 6 3 4 1 2 8 3
2
1 1000000000
3
0 1000000000 2

output

3
0
10
0
1

Note

For the first test cases the pairs are ( i , j ) (i, j) (i,j) = { ( 2 , 4 ) , ( 2 , 8 ) , ( 3 , 8 ) } \{(2, 4), (2, 8), (3, 8)\} {(2,4),(2,8),(3,8)}.

  • The pair ( 2 , 4 ) (2, 4) (2,4) is true because a 2 = 1 a_2 = 1 a2=1, a 4 = 3 a_4 = 3 a4=3 and 1 < 2 < 3 < 4 1 < 2 < 3 < 4 1<2<3<4.
  • The pair ( 2 , 8 ) (2, 8) (2,8) is true because a 2 = 1 a_2 = 1 a2=1, a 8 = 4 a_8 = 4 a8=4 and 1 < 2 < 4 < 8 1 < 2 < 4 < 8 1<2<4<8.
  • The pair ( 3 , 8 ) (3, 8) (3,8) is true because a 3 = 2 a_3 = 2 a3=2, a 8 = 4 a_8 = 4 a8=4 and 2 < 3 < 4 < 8 2 < 3 < 4 < 8 2<3<4<8.

Tutorial
从前往后遍历,遇到满足 a i < i a_i < i ai<i 的值就将其坐标 i i i存起来

在存每个坐标之前,需要对已经存起来的坐标进行判断,有多少个数字满足 j < a i j < a_i j<ai(其中 j j j 为数组中的坐标),此时直接用二分查找即可

Solution

import sys
from bisect import bisect_left
input = sys.stdin.readline

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    all = []
    for i, x in enumerate(a):
        if x <= i:
            ans += bisect_left(all, x)
            all.append(i + 1)
    print(ans)

G. Good Key, Bad Key

time limit per test: 3 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

There are n n n chests. The i i i-th chest contains a i a_i ai coins. You need to open all n n n chests in order from chest 1 1 1 to chest n n n.

There are two types of keys you can use to open a chest:

  • a good key, which costs k k k coins to use;
  • a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest i i i will do a i = ⌊ a i 2 ⌋ a_i = \lfloor{\frac{a_i}{2}\rfloor} ai=2ai, a i + 1 = ⌊ a i + 1 2 ⌋ , … , a n = ⌊ a n 2 ⌋ a_{i+1} = \lfloor\frac{a_{i+1}}{2}\rfloor, \dots, a_n = \lfloor \frac{a_n}{2}\rfloor ai+1=2ai+1,,an=2an;
  • any key (both good and bad) breaks after a usage, that is, it is a one-time use.

You need to use in total n n n keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.

During the process, you are allowed to go into debt; for example, if you have 1 1 1 coin, you are allowed to buy a good key worth k = 3 k=3 k=3 coins, and your balance will become − 2 -2 2 coins.

Find the maximum number of coins you can have after opening all n n n chests in order from chest 1 1 1 to chest n n n.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105; 0 ≤ k ≤ 1 0 9 0 \leq k \leq 10^9 0k109) — the number of chests and the cost of a good key respectively.

The second line of each test case contains n n n integers a i a_i ai ( 0 ≤ a i ≤ 1 0 9 0 \leq a_i \leq 10^9 0ai109) — the amount of coins in each chest.

The sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case output a single integer — the maximum number of coins you can obtain after opening the chests in order from chest 1 1 1 to chest n n n.

Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Example
input

5
4 5
10 10 3 1
1 2
1
3 12
10 10 29
12 51
5 74 89 45 18 69 67 67 11 96 23 59
2 57
85 60

output

11
0
13
60
58

Note

In the first test case, one possible strategy is as follows:

  • Buy a good key for 5 5 5 coins, and open chest 1 1 1, receiving 10 10 10 coins. Your current balance is 0 + 10 − 5 = 5 0 + 10 - 5 = 5 0+105=5 coins.
  • Buy a good key for 5 5 5 coins, and open chest 2 2 2, receiving 10 10 10 coins. Your current balance is 5 + 10 − 5 = 10 5 + 10 - 5 = 10 5+105=10 coins.
  • Use a bad key and open chest 3 3 3. As a result of using a bad key, the number of coins in chest 3 3 3 becomes ⌊ 3 2 ⌋ = 1 \left\lfloor \frac{3}{2} \right\rfloor = 1 23=1, and the number of coins in chest 4 4 4 becomes ⌊ 1 2 ⌋ = 0 \left\lfloor \frac{1}{2} \right\rfloor = 0 21=0. Your current balance is 10 + 1 = 11 10 + 1 = 11 10+1=11.
  • Use a bad key and open chest 4 4 4. As a result of using a bad key, the number of coins in chest 4 4 4 becomes ⌊ 0 2 ⌋ = 0 \left\lfloor \frac{0}{2} \right\rfloor = 0 20=0. Your current balance is 11 + 0 = 11 11 + 0 = 11 11+0=11.

At the end of the process, you have 11 11 11 coins, which can be proven to be maximal.

Tutorial
先全部用好钥匙,最后再用坏钥匙是最佳的,证明过程如下:

假设我们使用了一把坏钥匙,然后又使用了一把好钥匙,这样我们就获得了 ⌊ a i 2 ⌋ + ⌊ a i + 1 2 ⌋ − k \lfloor\frac{a_i}{2}\rfloor + \lfloor\frac{a_{i+1}}{2}\rfloor - k 2ai+2ai+1k 个硬币,如果我们先使用好钥匙,再使用坏钥匙,那么我们将会得到 a i + ⌊ a i + 1 2 ⌋ − k a_i + \lfloor\frac{a_{i+1}}{2}\rfloor - k ai+2ai+1k 个硬币,由此可见,先使用好钥匙,再使用坏钥匙会是最优解

所以我们只需要从前往后遍历,从每个数往后判断 30 30 30 个坏钥匙(因为数据范围为 0 ≤ a i ≤ 1 0 9 0 \leq a_i \leq 10^9 0ai109 ,而 log ⁡ 2 ( 1 0 9 ) ≈ 30 \log_2(10^9) \approx 30 log2(109)30,所以只需要往后判断 30 30 30 个数),求和判断最大值即可

Solution

import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    ans, cnt = sum(x >> (i + 1) for i, x in enumerate(a[:31])), 0
    for i in range(n):
        cnt += a[i] - k
        mid = 0
        for j in range(1, min(31, n - i)):
            mid += a[i + j] >> j
        ans = max(ans, cnt + mid)
    print(ans)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值