Educational Codeforces Round 161 (Rated for Div. 2) A B C D E

A. Tricky Template

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

You are given an integer n n n and three strings a , b , c a, b, c a,b,c, each consisting of n n n lowercase Latin letters.

Let a template be a string t t t consisting of n n n lowercase and/or uppercase Latin letters. The string s s s matches the template t t t if the following conditions hold for all i i i from 1 1 1 to n n n:

  • if the i i i-th letter of the template is lowercase, then s i s_i si must be the same as t i t_i ti;
  • if the i i i-th letter of the template is uppercase, then s i s_i si must be different from the lowercase version of t i t_i ti. For example, if there is a letter ‘A’ in the template, you cannot use the letter ‘a’ in the corresponding position of the string.

Accordingly, the string doesn’t match the template if the condition doesn’t hold for at least one i i i.

Determine whether there exists a template t t t such that the strings a a a and b b b match it, while the string c c c does not.

Input

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

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 20 1 \le n \le 20 1n20) — the length of the given strings.

The next three lines contain the strings a , b a, b a,b and c c c. Each string consists of exactly n n n lowercase Latin letters.

Output

For each testcase, print “YES” if there exists a template t t t such that the strings a a a and b b b match it, while the string c c c does not. Otherwise, print “NO”.

Example

input

4
1
a
b
c
2
aa
bb
aa
10
mathforces
luckforces
adhoccoder
3
acc
abd
abc

output

YES
NO
YES
NO

Note

In the first test case, you can use the template “C”. The first letter of strings a a a and b b b differ from ‘c’, so they match the template. The first letter of string c c c equals ‘c’, so it doesn’t match.

In the third test case, you can use the template “CODEforces”.

Tutorial

只需要字符串 c c c 存在一个位置与字符串 a a a 和字符串 b b b 都不同即可

Solution

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

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

void solve() {
    int n, cnt = 0;
    string a, b, c;
    cin >> n >> a >> b >> c;
    for (int i = 0; i < n; ++i) {
        cnt += (a[i] == c[i] or b[i] == c[i]);
    }
    cout << (cnt == n ? "NO" : "YES") << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

B. Forming Triangles

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

You have n n n sticks, numbered from 1 1 1 to n n n. The length of the i i i-th stick is 2 a i 2^{a_i} 2ai.

You want to choose exactly 3 3 3 sticks out of the given n n n sticks, and form a non-degenerate triangle out of them, using the sticks as the sides of the triangle. A triangle is called non-degenerate if its area is strictly greater than 0 0 0.

You have to calculate the number of ways to choose exactly 3 3 3 sticks so that a triangle can be formed out of them. Note that the order of choosing sticks does not matter (for example, choosing the 1 1 1-st, 2 2 2-nd and 4 4 4-th stick is the same as choosing the 2 2 2-nd, 4 4 4-th and 1 1 1-st stick).

Input

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

Each test case consists of two lines:

  • the first line contains one integer n n n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 1 \le n \le 3 \cdot 10^5 1n3105);
  • the second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 0 ≤ a i ≤ n 0 \le a_i \le n 0ain).

Additional constraint on the input: the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3105.

Output

For each test case, print one integer — the number of ways to choose exactly 3 3 3 sticks so that a triangle can be formed out of them.

Example

input

4
7
1 1 1 1 1 1 1
4
3 2 1 3
3
1 2 3
1
1

output

35
2
0
0

Note

In the first test case of the example, any three sticks out of the given 7 7 7 can be chosen.

In the second test case of the example, you can choose the 1 1 1-st, 2 2 2-nd and 4 4 4-th stick, or the 1 1 1-st, 3 3 3-rd and 4 4 4-th stick.

In the third test case of the example, you cannot form a triangle out of the given sticks with lengths 2 2 2, 4 4 4 and 8 8 8.

Tutorial

由题意可得,每根棍子长度为 2 a i 2^{a_i} 2ai,设三条边长度从小到大分别是 a i , a j , a k a_i, a_j, a_k ai,aj,ak,则 2 a i + 2 a j > 2 a k 2^{a_i} + 2^{a_j} > 2 ^ {a_k} 2ai+2aj>2ak,所以如果需要满足这个条件,就所取的三角形一定是等腰三角形,才能满足题意,设最长边长度为 x x x,比最长边短的边的个数为 s u m sum sum

  • 如果三条边的长度都相同,则有 c n t x × ( c n t x − 1 ) × ( c n t x − 2 ) 6 cnt_x \times (cnt_x - 1) \times (cnt_x - 2) \over 6 6cntx×(cntx1)×(cntx2) 种取法
  • 如果两条最长边相同,另取一条比它们小的边,有 c n t x × ( c n t x − 1 ) 2 × s u m {cnt_x \times (cnt_x - 1) \over 2} \times sum 2cntx×(cntx1)×sum 种取法

则答案可通过遍历一次计数表即可得到

Solution

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

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

void solve() {
    int n, ans = 0;
    cin >> n;
    vector<int> a(n);
    map<int, int> mp;
    for (int &ai : a) {
        cin >> ai;
        mp[ai]++;
    }
    int sum = 0;
    for (auto [x, y] : mp) {
        ans += y * (y - 1) * (y - 2) / 6 + sum * (y - 1) * y / 2;
        sum += y;
    }
    
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

C. Closest Cities

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

There are n n n cities located on the number line, the i i i-th city is in the point a i a_i ai. The coordinates of the cities are given in ascending order, so a 1 < a 2 < ⋯ < a n a_1 < a_2 < \dots < a_n a1<a2<<an.

The distance between two cities x x x and y y y is equal to ∣ a x − a y ∣ |a_x - a_y| axay.

For each city i i i, let’s define the closest city j j j as the city such that the distance between i i i and j j j is not greater than the distance between i i i and each other city k k k. For example, if the cities are located in points [ 0 , 8 , 12 , 15 , 20 ] [0, 8, 12, 15, 20] [0,8,12,15,20], then:

  • the closest city to the city 1 1 1 is the city 2 2 2;
  • the closest city to the city 2 2 2 is the city 3 3 3;
  • the closest city to the city 3 3 3 is the city 4 4 4;
  • the closest city to the city 4 4 4 is the city 3 3 3;
  • the closest city to the city 5 5 5 is the city 4 4 4.

The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points [ 1 , 2 , 3 ] [1, 2, 3] [1,2,3], since this would mean that the city 2 2 2 has two closest cities ( 1 1 1 and 3 3 3, both having distance 1 1 1).

You can travel between cities. Suppose you are currently in the city x x x. Then you can perform one of the following actions:

  • travel to any other city y y y, paying ∣ a x − a y ∣ |a_x - a_y| axay coins;
  • travel to the city which is the closest to x x x, paying 1 1 1 coin.

You are given m m m queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city.

Input

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

Each test case is given in the following format:

  • the first line contains one integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105);
  • the second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 0 ≤ a 1 < a 2 < ⋯ < a n ≤ 1 0 9 0 \le a_1 < a_2 < \dots < a_n \le 10^9 0a1<a2<<an109);
  • the third line contains one integer m m m ( 1 ≤ m ≤ 1 0 5 1 \le m \le 10^5 1m105);
  • then m m m lines follow; the i i i-th of them contains two integers x i x_i xi and y i y_i yi ( 1 ≤ x i , y i ≤ n 1 \le x_i, y_i \le n 1xi,yin; x i ≠ y i x_i \ne y_i xi=yi), denoting that in the i i i-th query, you have to calculate the minimum number of coins you have to spend to travel from the city x i x_i xi to the city y i y_i yi.

Additional constraints on the input:

  • in every test case, for each city, the closest city is determined uniquely;
  • the sum of n n n over all test cases does not exceed 1 0 5 10^5 105;
  • the sum of m m m over all test cases does not exceed 1 0 5 10^5 105.

Output

For each query, print one integer — the minimum number of coins you have to spend.

Example

input

1
5
0 8 12 15 20
5
1 4
1 5
3 4
3 2
5 1

output

3
8
1
4
14

Note

Let’s consider the first two queries in the example from the statement:

  • in the first query, you are initially in the city 1 1 1. You can travel to the closest city (which is the city 2 2 2), paying 1 1 1 coin. Then you travel to the closest city (which is the city 3 3 3) again, paying 1 1 1 coin. Then you travel to the closest city (which is the city 4 4 4) again, paying 1 1 1 coin. In total, you spend 3 3 3 coins to get from the city 1 1 1 to the city 4 4 4;
  • in the second query, you can use the same way to get from the city 1 1 1 to the city 4 4 4, and then spend 5 5 5 coins to travel from the city 4 4 4 to the city 5 5 5.

Tutorial

用两个数组分别记录当前位置向左走和向右走所需要的花费,然后对它们进行前缀和计算,即可通过方向判断,得到当前位置到目标位置到总花费

Solution

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

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

void solve() {
    int n, m, x, y;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    // 每个点 向右走的花费 和 向左走的花费
    vector<int> pre(n + 2), suf(n + 2);
    pre[1] = suf[n] = 1;
    for (int i = 2; i < n; ++i) {
        pre[i] = (a[i] - a[i - 1] < a[i + 1] - a[i] ? a[i + 1] - a[i] : 1);
    }
    for (int i = n - 1; i > 1; --i) {
        suf[i] = (a[i] - a[i - 1] > a[i + 1] - a[i] ? a[i] - a[i - 1] : 1);
    }
    for (int i = 1; i < n; ++i) {
        pre[i] += pre[i - 1];
    }
    for (int i = n; i > 0; --i) {
        suf[i] += suf[i + 1];
    }
    cin >> m;
    while (m--) {
        cin >> x >> y;
        if (x > y) {
            cout << suf[y + 1] - suf[x + 1] << endl;
        } else {
            cout << pre[y - 1] - pre[x - 1] << endl;
        }
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

D. Berserk Monsters

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

Monocarp is playing a computer game (yet again). Guess what is he doing? That’s right, killing monsters.

There are n n n monsters in a row, numbered from 1 1 1 to n n n. The i i i-th monster has two parameters: attack value equal to a i a_i ai and defense value equal to d i d_i di. In order to kill these monsters, Monocarp put a berserk spell on them, so they’re attacking each other instead of Monocarp’s character.

The fight consists of n n n rounds. Every round, the following happens:

  • first, every alive monster i i i deals a i a_i ai damage to the closest alive monster to the left (if it exists) and the closest alive monster to the right (if it exists);
  • then, every alive monster j j j which received more than d j d_j dj damage during this round dies. I. e. the j j j-th monster dies if and only if its defense value d j d_j dj is strictly less than the total damage it received this round.

For each round, calculate the number of monsters that will die during that round.

Input

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

Each test case consists of three lines:

  • the first line contains one integer n n n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 1 \le n \le 3 \cdot 10^5 1n3105);
  • the second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109);
  • the third line contains n n n integers d 1 , d 2 , … , d n d_1, d_2, \dots, d_n d1,d2,,dn ( 1 ≤ d i ≤ 1 0 9 1 \le d_i \le 10^9 1di109).

Additional constraint on the input: the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3105.

Output

For each test case, print n n n integers. The i i i-th integer should be equal to the number of monsters that die during the i i i-th round.

Example

input

3
5
3 4 7 5 10
4 9 1 18 1
2
2 1
1 3
4
1 1 2 4
3 3 4 2

output

3 1 0 0 0 
0 0 
1 1 1 0 

Note

Explanation for the first test case of the example:

During the first round, the following happens:

  • the monster 1 1 1 deals 3 3 3 damage to the monster 2 2 2;
  • the monster 2 2 2 deals 4 4 4 damage to the monster 1 1 1 and the monster 3 3 3;
  • the monster 3 3 3 deals 7 7 7 damage to the monster 2 2 2 and the monster 4 4 4;
  • the monster 4 4 4 deals 5 5 5 damage to the monster 3 3 3 and the monster 5 5 5;
  • the monster 5 5 5 deals 10 10 10 damage to the monster 4 4 4;
  • the monster 1 1 1 does not die, since it received 4 4 4 damage and its defense is 4 4 4;
  • the monster 2 2 2 dies, since it received 10 10 10 damage and its defense is 9 9 9;
  • the monster 3 3 3 dies, since it received 9 9 9 damage and its defense is 1 1 1;
  • the monster 4 4 4 does not die, since it received 17 17 17 damage and its defense is 18 18 18;
  • the monster 5 5 5 dies, since it received 5 5 5 damage and its defense is 1 1 1.

After the first round, the monsters 1 1 1 and 4 4 4 stay alive.

During the second round, the following happens:

  • the monster 1 1 1 deals 3 3 3 damage to the monster 4 4 4;
  • the monster 4 4 4 deals 5 5 5 damage to the monster 1 1 1;
  • the monster 1 1 1 dies, since it received 5 5 5 damage and its defense is 4 4 4;
  • the monster 4 4 4 does not die, since it received 3 3 3 damage and its defense is 18 18 18.

During the next three rounds, only the 4 4 4-th monster is alive, so nothing happens.

Tutorial

先记录每个怪物的左怪物和右怪物,在之后的每次死亡后,死亡怪物左右两边的怪物的左右怪物都会因此受到影响,此时,左怪物的右怪物会更新,右怪物的左怪物会更新

在每次攻击回合时,只需要对还存活的怪物进行判断即可,用 s e t set set 记录存活的怪物即可(一个怪物可能被左右两边的怪物存入 s e t set set 中)

Solution

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

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

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), d(n), l(n), r(n), ans(n), mid;
    vector<bool> vis(n);
    for (int &ai : a) {
        cin >> ai;
    }
    for (int &di : d) {
        cin >> di;
    }
    for (int i = 0; i < n; ++i) {
        l[i] = i - 1;
        r[i] = i + 1;
    }
    
    // 进行攻击
    function<int(int)> hack = [&](int id) -> int {
        int ans = 0;
        if (l[id] ^ -1) {
            ans += a[l[id]];
        }
        if (r[id] ^ n) {
            ans += a[r[id]];
        }
        return ans;
    };

    for (int i = 0; i < n; ++i) {
        int blood = hack(i);
        if (blood > d[i]) {
            mid.emplace_back(i);
        }
    }

    for (int i = 0; i < n; ++i) {
        set<int> update;
        ans[i] = mid.size();
        for (int id : mid) {
            vis[id] = true;
            if (l[id] ^ -1) {
                r[l[id]] = r[id];
                update.insert(l[id]);
            }
            if (r[id] ^ n) {
                l[r[id]] = l[id];
                update.insert(r[id]);
            }
        }
        mid.clear();

        for (int id : update) {
            if (vis[id]) {
                continue;
            }
            int blood = hack(id);
            if (blood > d[id]) {
                mid.emplace_back(id);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << ans[i] << " \n"[i == n - 1];
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

E. Increasing Subsequences

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

Let’s recall that an increasing subsequence of the array a a a is a sequence that can be obtained from it by removing some elements without changing the order of the remaining elements, and the remaining elements are strictly increasing (i. e a b 1 < a b 2 < ⋯ < a b k a_{b_1} < a_{b_2} < \dots < a_{b_k} ab1<ab2<<abk and b 1 < b 2 < ⋯ < b k b_1 < b_2 < \dots < b_k b1<b2<<bk). Note that an empty subsequence is also increasing.

You are given a positive integer X X X. Your task is to find an array of integers of length at most 200 200 200, such that it has exactly X X X increasing subsequences, or report that there is no such array. If there are several answers, you can print any of them.

If two subsequences consist of the same elements, but correspond to different positions in the array, they are considered different (for example, the array [ 2 , 2 ] [2, 2] [2,2] has two different subsequences equal to [ 2 ] [2] [2]).

Input

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

The only line of each test case contains a single integer X X X ( 2 ≤ X ≤ 1 0 18 2 \le X \le 10^{18} 2X1018).

Output

For each query, print the answer to it. If it is impossible to find the required array, print -1 on the first line. Otherwise, print a positive integer n n n on the first line — the length of the array. On the second line, print n n n integers — the required array itself. If there are several answers, you can print any of them. All elements of the array should be in the range [ − 1 0 9 ; 1 0 9 ] [-10^9; 10^9] [109;109].

Example

input

4
2
5
13
37

output

1
0
3
0 1 0
5
2 2 3 4 2
7
-1 -1 0 0 2 3 -1

Tutorial

c n t cnt cnt 为递增子序列个数,此时:

  • 在末尾添加 0,此时这个新加入的 0 是一个新的递增子序列, c n t cnt cnt 变为 c n t + 1 cnt + 1 cnt+1
  • 在末尾添加比最大值还大的数,此时这个数可以与前面所有递增子序列构成一个新的递增子序列,也可以自己构成一个新的递增子序列, c n t cnt cnt 变为 2 × c n t + 1 2 \times cnt + 1 2×cnt+1

我们可以利用逆向思维:

  • 如果 x x x 是偶数,此时可以在数组前面添加 0,然后 x x x 变为 x − 1 x - 1 x1
  • 如果 x x x 是奇数,此时可以在数组前面添加 c u r cur cur,然后 x x x 变为 x − 1 2 x - 1 \over 2 2x1 c u r cur cur 变为 c u r − 1 cur - 1 cur1
  • 注意:在此之前需要先把空子序列减去

Solution

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

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

void solve() {
    int x, cur = 100;
    cin >> x;
    vector<int> ans;
    x--;
    while (x) {
        if (x & 1) {
            ans.emplace_back(cur--);
            x >>= 1;
        } else {
            ans.emplace_back(0);
            --x;
        }
    }
    cout << ans.size() << endl;
    for (int i = ans.size() - 1; i >= 0; --i) {
        cout << ans[i] << " \n"[not i];
    }
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}
  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值