Codeforces Round 817 (Div. 4)


A. Spell Check

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

Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.

Today he wrote string s s s of length n n n consisting only of uppercase or lowercase Latin letters. He asks you to check if s s s is the correct spelling of his name.

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 test cases.

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

The second line of each test case contains a string s s s consisting of only uppercase or lowercase Latin characters.

Output

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

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

Example
input

10
5
Timur
5
miurT
5
Trumi
5
mriTu
5
timur
4
Timr
6
Timuur
10
codeforces
10
TimurTimur
5
TIMUR

output

YES
YES
YES
YES
NO
NO
NO
NO
NO
NO

Tutorial
将输入的字符串排序,直接与Timru(Timur名字的排序)做比较

Solution

for _ in range(int(input())):
    n= int(input())
    s = input().strip()
    s = "".join(sorted(list(s)))
    name = "Timru"
    print("YES" if s == name else "NO")

B. Colourblindness

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

Vasya has a grid with 2 2 2 rows and n n n columns. He colours each cell red, green, or blue.

Vasya is colourblind and can’t distinguish green from blue. Determine if Vasya will consider the two rows of the grid to be coloured the same.

Input

The input consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 100 1 \leq n \leq 100 1n100) — the number of columns of the grid.

The following two lines each contain a string consisting of n n n characters, each of which is either R, G, or B, representing a red, green, or blue cell, respectively — the description of the grid.

Output

For each test case, output “YES” if Vasya considers the grid’s two rows to be identical, and “NO” otherwise.

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

Example
input

6
2
RG
RB
4
GRBG
GBGB
5
GGGGG
BBBBB
7
BBBBBBB
RRRRRRR
8
RGBRRGBR
RGGRRBGR
1
G
G

output

YES
NO
YES
NO
YES
YES

Note

In the first test case, Vasya sees the second cell of each row as the same because the second cell of the first row is green and the second cell of the second row is blue, so he can’t distinguish these two cells. The rest of the rows are equal in colour. Therefore, Vasya will say that the two rows are coloured the same, even though they aren’t.

In the second test case, Vasya can see that the two rows are different.

In the third test case, every cell is green or blue, so Vasya will think they are the same.

Tutorial
将两行字符串中的G全部替换成B,最后直接两个字符串做比较

Solution

for _ in range(int(input())):
    n = int(input())
    g = [input().strip() for __ in range(2)]
    g[0] = g[0].replace('G', 'B')
    g[1] = g[1].replace('G', 'B')
    print("YES" if g[0] == g[1] else "NO")

C. Word Game

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

Three guys play a game: first, each person writes down n n n distinct words of length 3 3 3. Then, they total up the number of points as follows:

  • if a word was written by one person — that person gets 3 points,
  • if a word was written by two people — each of the two gets 1 point,
  • if a word was written by all — nobody gets any points.

In the end, how many points does each player have?

Input

The input consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 1000 1 \leq n \leq 1000 1n1000) — the number of words written by each person.

The following three lines each contain n n n distinct strings — the words written by each person. Each string consists of 3 3 3 lowercase English characters.

Output

For each test case, output three space-separated integers — the number of points each of the three guys earned. You should output the answers in the same order as the input; the i i i-th integer should be the number of points earned by the i i i-th guy.

Example
input

3
1
abc
def
abc
3
orz for qaq
qaq orz for
cod for ces
5
iat roc hem ica lly
bac ter iol ogi sts
bac roc lly iol iat

output

1 3 1 
2 2 6 
9 11 5 

Note

In the first test case:

  • The word abc \texttt{abc} abc was written by the first and third guys — they each get 1 1 1 point.
  • The word def \texttt{def} def was written by the second guy only — he gets 3 3 3 points.

Tutorial
先将每个人输入的字符串都存在一个计数器中,然后一次遍历每个人写的每个单词是否有被他人写过,根据写过这个单词的人进行加分

Solution

for _ in range(int(input())):
    n = int(input())
    a = [list(map(str, input().split())) for __ in range(3)]
    mp = [Counter() for __ in range(3)]
    ans = [0 for __ in range(3)]
    for i in range(3):
        for c in a[i]:
            mp[i][c] += 1
    for i in range(3):
        for c in a[i]:
            if mp[i][c]:
                cnt = sum(mp[(i + j) % 3][c] > 0 for j in [1, 2])
                if cnt == 0:
                    ans[i] += 3
                elif cnt == 1:
                    ans[i] += 1
    print(*ans)

D. Line

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

There are n n n people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person’s count.

For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are [ 0 , 3 , 2 , 3 , 4 ] [0, 3, 2, 3, 4] [0,3,2,3,4], and the value is 0 + 3 + 2 + 3 + 4 = 12 0+3+2+3+4=12 0+3+2+3+4=12.

You are given the initial arrangement of people in the line. For each k k k from 1 1 1 to n n n, determine the maximum value of the line if you can change the direction of at most k k k people.

Input

The input consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases. The description of the test cases follows.

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

The following line contains a string consisting of n n n characters, each of which is either L or R, representing a person facing left or right, respectively — the description of the line.

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

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++).

Output

For each test case, output n n n space-separated non-negative integers — the maximum value of the line if you can change the direction of at most k k k people for each k k k from 1 1 1 to n n n, inclusive.

Example
input

6
3
LLR
5
LRRLL
1
L
12
LRRRLLLRLLRL
10
LLLLLRRRRR
9
LRLRLRLRL

output

3 5 5 
16 16 16 16 16 
0 
86 95 98 101 102 102 102 102 102 102 102 102 
29 38 45 52 57 62 65 68 69 70 
44 50 54 56 56 56 56 56 56 

Note

In the first test case:

  • k = 1 k=1 k=1: change the direction of 1 1 1 person to make the line RLR. The total value is 2 + 1 + 0 = 3 2+1+0=3 2+1+0=3.
  • k = 2 k=2 k=2: change the direction of 2 2 2 people to make the line RLL. The total value is 2 + 1 + 2 = 5 2+1+2=5 2+1+2=5.
  • k = 3 k=3 k=3: change the direction of 2 2 2 people to make the line RLL. The total value is 2 + 1 + 2 = 5 2+1+2=5 2+1+2=5. Note that you have to change the direction of at most k k k people.

In the second test case, it is optimal to only change the direction of the first person for all k k k from 1 1 1 to 5 5 5 (that is, make the line RRRLL).

Tutorial
将每个位置可以加的分数存起来,于此同时统计初始分数,然后将可以加的分从大到小排序,依次加入到分数中输出

Solution

for _ in range(int(input())):
    n = int(input()) - 1
    l, r, ans = 0, n, 0
    s = input().strip()
    all = []
    for i, c in enumerate(s):
        if c == 'L':
            ans += i
            all.append(max(0, n - 2 * i))
        else:
            ans += n - i
            all.append(max(0, 2 * i - n))
    all.sort(reverse=True)
    for a in all:
        ans += a
        print(ans, end=" ")
    print()

E. Counting Rectangles

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

You have n n n rectangles, the i i i-th rectangle has height h i h_i hi and width w i w_i wi.

You are asked q q q queries of the form h s   w s   h b   w b h_s \ w_s \ h_b \ w_b hs ws hb wb.

For each query output, the total area of rectangles you own that can fit a rectangle of height h s h_s hs and width w s w_s ws while also fitting in a rectangle of height h b h_b hb and width w b w_b wb. In other words, print ∑ h i ⋅ w i \sum h_i \cdot w_i hiwi for i i i such that h s < h i < h b h_s < h_i < h_b hs<hi<hb and w s < w i < w b w_s < w_i < w_b ws<wi<wb.

Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.

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++).

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 test cases.

The first line of each test case two integers n , q n, q n,q ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105; 1 ≤ q ≤ 1 0 5 1 \leq q \leq 10^5 1q105) — the number of rectangles you own and the number of queries.

Then n n n lines follow, each containing two integers h i , w i h_i, w_i hi,wi ( 1 ≤ h i , w i ≤ 1000 1 \leq h_i, w_i \leq 1000 1hi,wi1000) — the height and width of the i i i-th rectangle.

Then q q q lines follow, each containing four integers h s , w s , h b , w b h_s, w_s, h_b, w_b hs,ws,hb,wb ( 1 ≤ h s < h b ,   w s < w b ≤ 1000 1 \leq h_s < h_b,\ w_s < w_b \leq 1000 1hs<hb, ws<wb1000) — the description of each query.

The sum of q q q over all test cases does not exceed 1 0 5 10^5 105, and 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 q q q lines, the i i i-th line containing the answer to the i i i-th query.

Example
input

3
2 1
2 3
3 2
1 1 3 4
5 5
1 1
2 2
3 3
4 4
5 5
3 3 6 6
2 1 4 5
1 1 2 10
1 1 100 100
1 1 3 3
3 1
999 999
999 999
999 998
1 1 1000 1000

output

6
41
9
0
54
4
2993004

Note

In the first test case:

  • k = 1 k=1 k=1: change the direction of 1 1 1 person to make the line RLR. The total value is 2 + 1 + 0 = 3 2+1+0=3 2+1+0=3.
  • k = 2 k=2 k=2: change the direction of 2 2 2 people to make the line RLL. The total value is 2 + 1 + 2 = 5 2+1+2=5 2+1+2=5.
  • k = 3 k=3 k=3: change the direction of 2 2 2 people to make the line RLL. The total value is 2 + 1 + 2 = 5 2+1+2=5 2+1+2=5. Note that you have to change the direction of at most k k k people.

In the second test case, it is optimal to only change the direction of the first person for all k k k from 1 1 1 to 5 5 5 (that is, make the line RRRLL).

Tutorial
本题其实就是一道经典的二维前缀和,难点就在于如何将实际问题抽象化

我们可以将 长 和 宽 抽象成坐标轴上的 x 和 y

每一个 长 和 宽 都对应着一个面积,而这个面积就是这个点上权值,判断所有可以装进矩形的面积时直接通过二维前缀和判断范围内的总面积即可

Solution

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

#define endl '\n'
#define int long long

void solve() {
    int n, q, h1, w1, h2, w2;
    cin >> n >> q;
    int g[1005][1005];
    memset(g, 0, sizeof(g));
    while (n--) {
        cin >> h1 >> w1;
        g[h1][w1] += h1 * w1;
    }
    for (int i = 1; i < 1005; ++i) {
        for (int j = 1; j < 1005; ++j) {
            g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
        }
    }
    while (q--) {
        cin >> h1 >> w1 >> h2 >> w2;
        --h2, --w2;
        cout << g[h2][w2] - g[h1][w2] - g[h2][w1] + g[h1][w1] << endl;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

F. L-shapes

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

An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way.

You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can’t touch an edge or corner. More formally:

  • Each shaded cell in the grid is part of exactly one L-shape, and
  • no two L-shapes are adjacent by edge or corner.

For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively.

Input

The input consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and m m m ( 1 ≤ n , m ≤ 50 1 \leq n, m \leq 50 1n,m50) — the number of rows and columns in the grid, respectively.

Then n n n lines follow, each containing m m m characters. Each of these characters is either ‘.’ or ‘*’ — an empty cell or a shaded cell, respectively.

Output

For each test case, output “YES” if the grid is made up of L-shape that don’t share edges or corners, and “NO” otherwise.

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

Example
input

10
6 10
........**
.**......*
..*..*....
.....**...
...*.....*
..**....**
6 10
....*...**
.**......*
..*..*....
.....**...
...*.....*
..**....**
3 3
...
***
...
4 4
.*..
**..
..**
..*.
5 4
.*..
**..
....
..**
..*.
3 2
.*
**
*.
2 3
*..
.**
3 2
..
**
*.
3 3
.**
*.*
**.
3 3
..*
.**
..*

output

YES
NO
NO
NO
YES
NO
NO
YES
NO
NO

Tutorial
首先我们需要判断连通块的个数并判断是否有拐角,在遇到 ∗ * 后如果它还没有被标记过,那么则对其进行 D F S DFS DFS,但是连通块是否有且仅有3个方格容易判断,但如何判断这个点是这个 L − s h a p e L-shape Lshape 的拐角呢,此时用位运算即可解决这个问题,只需要根据 左、右、上、下 的顺序依次判断,用 d i r e c t i o n ∣ = ( 1 < < ( k > > 1 ) ) direction |= (1 << (k >> 1)) direction=(1<<(k>>1)) 这个式子判断连通块往这个点的那个方向延伸,最后会发现当 d i r e c t i o n = 3 direction = 3 direction=3 时就代表这个点是这个连通块的拐点,只要通过拐点和这个连通块所包含的数量,就可将这个 L − s h a p e L-shape Lshape 进行标记

最后判断每个阴影部分,如果不属于 L − s h a p e L-shape Lshape 或周围存在不是本连通块的阴影,即返回 N O NO NO

Solution

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

#define endl '\n'
#define int long long

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

void solve() {
    int n, m, num = 0;
    cin >> n >> m;
    vector<string> g(n);
    vector memo(n, vector<int>(m, 0));
    for (string &gi : g) {
        cin >> gi;
    }

    function<bool(int, int)> check = [&](int x, int y) -> bool {
        return x >= 0 and x < n and y >= 0 and y < m and g[x][y] == '*';
    };

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (g[i][j] == '*' and not memo[i][j]) {
                int cnt = 0, direction = 0;
                for (int k = 0; k < 4; ++k) {
                    int x = i + dx[k], y = j + dy[k];
                    if (check(x, y)) {
                        cnt++;
                        direction |= (1 << (k >> 1));
                    }
                }
                if (cnt == 2 and direction == 3) {
                    memo[i][j] = ++num;
                    for (int k = 0; k < 4; ++k) {
                        int x = i + dx[k], y = j + dy[k];
                        if (check(x, y)) {
                            memo[x][y] = num;
                        }
                    }
                }
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (g[i][j] == '*') {
                if (!memo[i][j]) {
                    cout << "NO\n";
                    return;
                }
                for (int x = -1; x <= 1; ++x) {
                    for (int y = -1; y <= 1; ++y) {
                        int nx = i + x, ny = j + y;
                        if (check(nx, ny) and memo[nx][ny] != memo[i][j]) {
                            cout << "NO\n";
                            return;
                        }
                    }
                }
            }
        }
    }
    cout << "YES\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

G. Even-Odd XOR

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

Given an integer n n n, find any array a a a of n n n distinct nonnegative integers less than 2 31 2^{31} 231 such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.

Input

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

Then t t t lines follow, each containing a single integer n n n ( 3 ≤ n ≤ 2 ⋅ 1 0 5 ) (3 \leq n \leq 2\cdot10^5) (3n2105) — the length of the array.

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

Output

For each test case, output one line containing n n n distinct integers that satisfy the conditions.

If there are multiple answers, you can output any of them.

Example
input

7
8
3
4
5
6
7
9

output

4 2 1 5 0 6 7 3
2 1 3
2 1 3 0
2 0 4 5 3
4 1 2 12 3 8
1 2 3 4 5 6 7
8 2 3 7 4 0 5 6 9

Note

In the first test case the XOR on odd indices is 4 ⊕ 1 ⊕ 0 ⊕ 7 = 2 4 \oplus 1 \oplus 0 \oplus 7 = 2 4107=2 and the XOR on even indices is 2 ⊕ 5 ⊕ 6 ⊕ 3 = 2 2 \oplus 5 \oplus 6 \oplus 3= 2 2563=2.

Tutorial
偶数索引数和奇数索引数的异或相等相当于所有元素的异或等于0

如果任意生成前 n − 1 n - 1 n1 个元素,最后一个元素设为前 n − 1 n - 1 n1 个元素的异或的异或即可,但此时不能保证所有元素都不同,此时我们可以任意生成前 n − 2 n - 2 n2 个元素,第 n − 1 n - 1 n1 个元素就可以设置为 2 30 2^{30} 230 ,最后一个数字就可以是前 n − 1 n - 1 n1 个元素的异或的异或

但此时怎么保证第 n − 2 n - 2 n2 个元素和第 n − 1 n - 1 n1 个元素不相等呢?我们可以直接在前 n − 2 n - 2 n2 个元素中选一个元素更改成其他不同的元素即可

Solution

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

#define endl '\n'
#define int long long

void solve() {
    int n;
    cin >> n;
    int all = 0, mid = (1ll << 31) - 1;
    for (int i = 0; i < n - 2; ++i) {
        all ^= i;
    }
    if (all) {
        for (int i = 0; i < n - 2; ++i) {
            cout << i << " ";
        }
        cout << (all ^ mid) << " " << mid << endl;
    } else {
        for (int i = 1; i < n - 1; ++i) {
            cout << i << " ";
        }
        cout << (all ^ (n - 2) ^ mid) << " " << mid << endl;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int test; cin >> test; while (test--)
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值