Codeforces Round 935 (Div. 3) A B C D E

A. Setting up Camp

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

The organizing committee plans to take the participants of the Olympiad on a hike after the tour. Currently, the number of tents needed to be taken is being calculated. It is known that each tent can accommodate up to 3 3 3 people.

Among the participants, there are a a a introverts, b b b extroverts, and c c c universals:

  • Each introvert wants to live in a tent alone. Thus, a tent with an introvert must contain exactly one person — only the introvert himself.
  • Each extrovert wants to live in a tent with two others. Thus, the tent with an extrovert must contain exactly three people.
  • Each universal is fine with any option (living alone, with one other person, or with two others).

The organizing committee respects the wishes of each participant very much, so they want to fulfill all of them.

Tell us the minimum number of tents needed to be taken so that all participants can be accommodated according to their preferences. If it is impossible to accommodate the participants in a way that fulfills all the wishes, output − 1 -1 1.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. This is followed by the descriptions of the test cases.

Each test case is described by a single line containing three integers a a a, b b b, c c c ( 0 ≤ a , b , c ≤ 1 0 9 0 \le a, b, c \le 10^9 0a,b,c109) — the number of introverts, extroverts, and universals, respectively.

Output

For each test case, output a single integer — the minimum number of tents, or − 1 -1 1 if it is impossible to accommodate the participants.

Example

input

10
1 2 3
1 4 1
1 4 2
1 1 1
1 3 2
19 7 18
0 0 0
7 0 0
0 24 0
1000000000 1000000000 1000000000

output

3
-1
3
-1
3
28
0
7
8
1666666667

Note

In the first test case, 1 1 1 tent will be given to the introverts, 1 1 1 tent will be shared by two extroverts and one universal, and the last tent will be shared by two universals. In total, 3 3 3 tents are needed.

In the second test case, three extroverts will take 1 1 1 tent, and 1 1 1 tent will be taken by an introvert. Then, one extrovert and one universal will be left. This extrovert will not be able to live with two others.

Tutorial

对于每一个社恐肯定都是一人一个帐篷,即 a a a 个帐篷,然后优先将社牛三个三个分到一起

  • 如果此时社牛刚好分配完毕,则将普通人三个三个分到一起,最后若有剩余也组成一个帐篷,即 b 3 b \over 3 3b 个帐篷住社牛 ⌈ c 3 ⌉ \lceil {c \over 3} \rceil 3c 个帐篷住普通人

  • 如果社牛没有被分配完,则有 ⌊ b 3 ⌋ \lfloor {b \over 3} \rfloor 3b 个帐篷全部住社牛,其他社牛和普通人一起住

    • 如果此时剩余的社牛和所有普通人都不够三个人,则此时社牛不能接受, a n s ans ans 即为 − 1 -1 1
    • 否则就将剩余的社牛普通人住一起,再剩余的普通人住一起,会有 ⌈ ( b m o d    3 ) + c 3 ⌉ \lceil {{(b \mod 3) + c} \over 3} \rceil 3(bmod3)+c

Solution

for _ in range(int(input())):
    a, b, c = map(int, input().split())
    a += b // 3
    b %= 3
    if b and b + c < 3:
        print(-1)
        continue
    print(a + (b + c + 2) // 3)

B. Fireworks

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

One of the days of the hike coincided with a holiday, so in the evening at the camp, it was decided to arrange a festive fireworks display. For this purpose, the organizers of the hike bought two installations for launching fireworks and a huge number of shells for launching.

Both installations are turned on simultaneously. The first installation launches fireworks every a a a minutes (i.e., after a , 2 ⋅ a , 3 ⋅ a , … a, 2 \cdot a, 3 \cdot a, \dots a,2a,3a, minutes after launch). The second installation launches fireworks every b b b minutes (i.e., after b , 2 ⋅ b , 3 ⋅ b , … b, 2 \cdot b, 3 \cdot b, \dots b,2b,3b, minutes after launch).

Each firework is visible in the sky for m + 1 m + 1 m+1 minutes after launch, i.e., if a firework was launched after x x x minutes after the installations were turned on, it will be visible every minute from x x x to x + m x + m x+m, inclusive. If one firework was launched m m m minutes after another, both fireworks will be visible for one minute.

What is the maximum number of fireworks that could be seen in the sky at the same time?

Input

Each test consists of several test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Then follow the descriptions of the test cases.

The first and only line of each test case contains integers a a a, b b b, m m m ( 1 ≤ a , b , m ≤ 1 0 18 1 \le a, b, m \le 10^{18} 1a,b,m1018) — the frequency of launching for the first installation, the second installation, and the time the firework is visible in the sky.

Output

For each set of input data, output a single number — the maximum number of fireworks that can be seen simultaneously.

Example

input

6
6 7 4
3 4 10
7 8 56
5 6 78123459896
1 1 1
1 1 1000000000000000000

output

2
7
17
28645268630
4
2000000000000000002

Note

In the first set of input data, the fireworks are visible in the sky for 5 5 5 minutes. Since the first installation launches fireworks every 6 6 6 minutes, and the second one every 7 7 7 minutes, two fireworks launched from the same installation will not be visible in the sky at the same time. At the same time, after 7 7 7 minutes from the start of the holiday, one firework from the first and one from the second camp will be visible. Thus, it is possible to see no more than 2 2 2 fireworks simultaneously.

In the third set of input data, 17 17 17 fireworks will be visible after 112 112 112 minutes:

  • 9 9 9 fireworks launched from the first installation at times [ 56 , 63 , 70 , 77 , 84 , 91 , 98 , 105 , 112 56, 63, 70, 77, 84, 91, 98, 105, 112 56,63,70,77,84,91,98,105,112];
  • 8 8 8 fireworks launched from the second installation at times [ 56 , 64 , 72 , 80 , 88 , 96 , 104 , 112 56, 64, 72, 80, 88, 96, 104, 112 56,64,72,80,88,96,104,112].

Tutorial

思维题:答案为 m m m 范围内 a a a 的倍数的个数和 b b b 的倍数的个数的总和加上 2 2 2

Solution

for _ in range(int(input())):
    a, b, m = map(int, input().split())
    print(m // a + m // b + 2)

C. Left and Right Houses

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

In the village of Letovo, there are n n n houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an, where a j = 0 a_j = 0 aj=0 if the resident of the j j j-th house wants to live on the left side of the street; otherwise, a j = 1 a_j = 1 aj=1.

The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses i i i and i + 1 i+1 i+1. Then the houses at positions between 1 1 1 and i i i will be on the left side of the street, and at positions between i + 1 i+1 i+1 and n n n will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively.

To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among x x x residents on one side, at least ⌈ x 2 ⌉ \lceil\frac{x}{2}\rceil 2x should want to live on that side, where ⌈ x ⌉ \lceil x \rceil x denotes rounding up a real number x x x.

To the left of the road, there will be i i i houses, among the corresponding a j a_j aj there must be at least ⌈ i 2 ⌉ \lceil\frac{i}{2}\rceil 2i zeros. To the right of the road, there will be n − i n-i ni houses, among the corresponding a j a_j aj there must be at least ⌈ n − i 2 ⌉ \lceil\frac{n-i}{2}\rceil 2ni ones.

Determine after which house i i i the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions i i i, minimize ∣ n 2 − i ∣ \left|\frac{n}{2} - i\right| 2ni .

If there are multiple suitable positions i i i with the minimum ∣ n 2 − i ∣ \left|\frac{n}{2} - i\right| 2ni , output the smaller one.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \le t \le 2\cdot 10^4 1t2104). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 3 ⋅ 1 0 5 3 \le n \le 3\cdot 10^5 3n3105). The next line of each test case contains a string a a a of length n n n, consisting only of 0 0 0 and 1 1 1.

It is guaranteed that 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, output a single number i i i — the position of the house after which the road should be laid (if it should be laid before the first house, output 0 0 0). We can show that the answer always exists.

Example

input

7
3
101
6
010111
6
011001
3
000
3
110
3
001
4
1100

output

2
3
2
3
0
1
0

Note

Let’s consider the first example of input data.

If we lay the road after the first house, there will be one house a 1 = 1 a_1 = 1 a1=1 on the left side of the street, the resident of which would like to live on the right side of the street. Then 0 0 0 out of 1 1 1 residents on the even side will be satisfied with the choice, which means that the road cannot be laid after house 1 1 1.

If we lay the road after the second house, 1 1 1 out of 2 2 2 residents on the left side (with preferences a 1 = 1 a_1 = 1 a1=1, a 2 = 0 a_2 = 0 a2=0) and 1 1 1 out of 1 1 1 resident on the right side (with preference a 3 = 1 a_3 = 1 a3=1) will be satisfied with the choice. More than half of the residents on each side are satisfied with the choice, which means that the road can be laid after house 2 2 2. We can show that this is the optimal answer.

Tutorial

利用前缀和算出每个区间内的 1 的个数,再从左到右遍历,根据题目要求判断当前位置之前和之后的 01 的个数是否满足题意

在判断时可以取巧,例如 i − a i < ⌈ i + 1 2 ⌉ i - a_i < \lceil {{i + 1} \over 2} \rceil iai<2i+1 可以写成 ( i − a i ) × 2 < i (i - a_i) \times 2 < i (iai)×2<i a n − a i < ⌈ n − i + 1 2 ⌉ a_n - a_i < \lceil {{n - i + 1} \over 2} \rceil anai<2ni+1 可以写成 ( a n − a i ) × 2 < n − i (a_n - a_i) \times 2 < n - i (anai)×2<ni ∣ n 2 − i ∣ \vert {n \over 2} - i \vert 2ni 可以用 ∣ n − i × 2 ∣ \vert n - i \times 2 \vert ni×2∣ 来表示

Solution

for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    a = [0 for __ in range(n + 1)]
    for i in range(n):
        a[i + 1] = a[i] + (s[i] == '1')
    cnt, ans = 0x3f3f3f3f, 0
    for i in range(n + 1):
        if (i - a[i]) * 2 < i or (a[-1] - a[i]) * 2 < n - i:
            continue
        if abs(n - 2 * i) < cnt:
            cnt = abs(n - 2 * i)
            ans = i
    print(ans)

D. Seraphim the Owl

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

The guys lined up in a queue of n n n people, starting with person number i = 1 i = 1 i=1, to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the n n n-th person. Kirill is completely dissatisfied with this situation, so he decided to bribe some people ahead of him.

For the i i i-th person in the queue, Kirill knows two values: a i a_i ai and b i b_i bi. If at the moment Kirill is standing at position i i i, then he can choose any position j j j such that j < i j < i j<i and exchange places with the person at position j j j. In this case, Kirill will have to pay him a j a_j aj coins. And for each k k k such that j < k < i j < k < i j<k<i, Kirill will have to pay b k b_k bk coins to the person at position k k k. Kirill can perform this action any number of times.

Kirill is thrifty, so he wants to spend as few coins as possible, but he doesn’t want to wait too long, so Kirill believes he should be among the first m m m people in line.

Help Kirill determine the minimum number of coins he will have to spend in order to not wait too long.

Input

Each test consists of several sets of input data. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases. Then follows the description of the test case.

The first line of each test case contains two integers n n n and m m m ( 1 ≤ m ≤ n ≤ 200   000 1 \le m \le n \le 200\,000 1mn200000) — the number of people in the queue besides Kirill and the maximum allowable final position of Kirill, respectively.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an separated by spaces ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109).

The third line contains n n n integers b 1 , b 2 , … , b n b_1, b_2, \dots, b_n b1,b2,,bn separated by spaces ( 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1bi109).

It is guaranteed that the sum of the values 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 a single integer — the minimum number of coins Kirill needs to spend.

Example

input

4
4 2
7 3 6 9
4 3 8 5
6 2
6 9 7 1 8 3
5 8 8 1 4 1
7 7
7 2 9 2 6 5 9
9 1 10 7 1 4 9
2 1
2 3
1 1

output

14
22
9
3

Tutorial

由题意可得,在 m m m 之后的每个位置,都要取 a i a_i ai 或者 b i b_i bi,所以这些位置取最小值就是最优解,即 ∑ i = m + 1 n min ⁡ ( a i , b i ) \sum^{n}_{i = m + 1}\min(a_i, b_i) i=m+1nmin(ai,bi)​,在 m m m 及之前的位置,如果到了 j j j 位置,则 j j j 位置取 a j a_j aj j j j m m m 之间的位置选 b x b_x bx,所以在这一段区间的值取 min ⁡ 1 ≤ i ≤ m ( a i + ∑ j = i + 1 m b j ) \min_{1 \leq i \leq m}(a_i + \sum^m_{j = i + 1}b_j) min1im(ai+j=i+1mbj)

综上所述,最后答案为 min ⁡ 1 ≤ i ≤ m ( a i + ∑ j = i + 1 m b j ) + ∑ i = m + 1 n min ⁡ ( a i , b i ) \min_{1 \leq i \leq m}(a_i + \sum^m_{j = i + 1}b_j) + \sum^{n}_{i = m + 1}\min(a_i, b_i) min1im(ai+j=i+1mbj)+i=m+1nmin(ai,bi)

Solution

for _ in range(int(input())):
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans, cnt = 200000000000001, 0
    for i in range(m - 1, -1, -1):
        ans = min(ans, cnt + a[i])
        cnt += b[i]
    for i in range(m, n):
        ans += min(a[i], b[i])
    print(ans)

E. Binary Search

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

Anton got bored during the hike and wanted to solve something. He asked Kirill if he had any new problems, and of course, Kirill had one.

You are given a permutation p p p of size n n n, and a number x x x that needs to be found. A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

You decided that you are a cool programmer, so you will use an advanced algorithm for the search — binary search. However, you forgot that for binary search, the array must be sorted.

You did not give up and decided to apply this algorithm anyway, and in order to get the correct answer, you can perform the following operation no more than 2 2 2 times before running the algorithm: choose the indices i i i, j j j ( 1 ≤ i , j ≤ n 1\le i, j \le n 1i,jn) and swap the elements at positions i i i and j j j.

After that, the binary search is performed. At the beginning of the algorithm, two variables l = 1 l = 1 l=1 and r = n + 1 r = n + 1 r=n+1 are declared. Then the following loop is executed:

  1. If r − l = 1 r - l = 1 rl=1, end the loop
  2. m = ⌊ r + l 2 ⌋ m = \lfloor \frac{r + l}{2} \rfloor m=2r+l
  3. If p m ≤ x p_m \le x pmx, assign l = m l = m l=m, otherwise r = m r = m r=m.

The goal is to rearrange the numbers in the permutation before the algorithm so that after the algorithm is executed, p l p_l pl is equal to x x x. It can be shown that 2 2 2 operations are always sufficient.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \le t \le 2\cdot 10^4 1t2104) — the number of test cases. Then follow the descriptions of the test cases.

The first line of each test case contains two integers n n n and x x x ( 1 ≤ x ≤ n ≤ 2 ⋅ 1 0 5 1 \le x \le n \le 2\cdot 10^5 1xn2105) — the length of the permutation and the number to be found.

The second line contains the permutation p p p separated by spaces ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin).

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

Output

For each test case, output an integer k k k ( 0 ≤ k ≤ 2 0 \le k \le 2 0k2) on the first line — the number of operations performed by you. In the next k k k lines, output 2 2 2 integers i i i, j j j ( 1 ≤ i , j ≤ n 1 \le i, j \le n 1i,jn) separated by a space, indicating that you are swapping the elements at positions i i i and j j j.

Note that you do not need to minimize the number of operations.

Example

input

5
6 3
1 2 3 4 5 6
6 5
3 1 6 5 2 4
5 1
3 5 4 2 1
6 3
4 3 1 5 2 6
3 2
3 2 1

output

0
1
3 4
2
2 4
1 5
2
4 5
2 4
1
1 3

Tutorial

根据题意对给定的数组进行二分,如果最后停留的位置是答案,那么直接输出 0,否则直接交换最终位置和原来 x x x 所在位置的元素

Solution

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

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

const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7; // 998244353;

void solve() {
    int n, x, idx = -1;
    cin >> n >> x;
    vector<int> p(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        if (p[i] == x) {
            idx = i;
        }
    }
    int left = 1, right = n + 1;
    while (left + 1 < right) {
        int mid = (left + right) >> 1;
        (p[mid] <= x ? left : right) = mid;
    }
    if (idx == left) {
        cout << 0 << endl;
        return;
    }
    cout << 1 << endl;
    cout << idx << " " << left << endl;
}

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

  • 32
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值