Codeforces Round 799 (Div. 4)


A. Marathon

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

You are given four distinct integers a a a, b b b, c c c, d d d.

Timur and three other people are running a marathon. The value a a a is the distance that Timur has run and b b b, c c c, d d d correspond to the distances the other three participants ran.

Output the number of participants in front of Timur.

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 description of each test case consists of four distinct integers a a a, b b b, c c c, d d d ( 0 ≤ a , b , c , d ≤ 1 0 4 0 \leq a, b, c, d \leq 10^4 0a,b,c,d104).

Output

For each test case, output a single integer — the number of participants in front of Timur.

Example
input

4
2 3 4 1
10000 0 1 2
500 600 400 300
0 9999 10000 9998

output

2
0
1
3

Note

For the first test case, there are 2 2 2 people in front of Timur, specifically the participants who ran distances of 3 3 3 and 4 4 4. The other participant is not in front of Timur because he ran a shorter distance than Timur.

For the second test case, no one is in front of Timur, since he ran a distance of 10000 10000 10000 while all others ran a distance of 0 0 0, 1 1 1, and 2 2 2 respectively.

For the third test case, only the second person is in front of Timur, who ran a total distance of 600 600 600 while Timur ran a distance of 500 500 500.

Tutorial
判断 b,c,d 大于 a 的个数

Solution

for _ in range(int(input())):
    a, b, c, d = map(int, input().split())
    print(sum(x > a for x in [b, c, d]))

B. All Distinct

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

Sho has an array a a a consisting of n n n integers. An operation consists of choosing two distinct indices i i i and j j j and removing a i a_i ai and a j a_j aj from the array.

For example, for the array [ 2 , 3 , 4 , 2 , 5 ] [2, 3, 4, 2, 5] [2,3,4,2,5], Sho can choose to remove indices 1 1 1 and 3 3 3. After this operation, the array becomes [ 3 , 2 , 5 ] [3, 2, 5] [3,2,5]. Note that after any operation, the length of the array is reduced by two.

After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible.

More formally, the array after Sho has made his operations respects these criteria:

  • No pairs such that ( i < j i < j i<j) and a i = a j a_i = a_j ai=aj exist.
  • The length of a a a is maximized.

Output the length of the final array.

Input

The first line contains a single 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 a single integer n n n ( 1 ≤ n ≤ 50 1 \leq n \leq 50 1n50) — the length of the array.

The second line of each test case contains n n n integers a i a_i ai ( 1 ≤ a i ≤ 1 0 4 1 \leq a_i \leq 10^4 1ai104) — the elements of the array.

Output

For each test case, output a single integer — the length of the final array. Remember that in the final array, all elements are different, and its length is maximum.

Example
input

4
6
2 2 2 3 3 3
5
9 1 9 9 1
4
15 16 16 15
4
10 100 1000 10000

output

2
1
2
4

Note

For the first test case Sho can perform operations as follows:

  1. Choose indices 1 1 1 and 5 5 5 to remove. The array becomes [ 2 , 2 , 2 , 3 , 3 , 3 ] → [ 2 , 2 , 3 , 3 ] [2, 2, 2, 3, 3, 3] \rightarrow [2, 2, 3, 3] [2,2,2,3,3,3][2,2,3,3].
  2. Choose indices 1 1 1 and 4 4 4 to remove. The array becomes [ 2 , 2 , 3 , 3 ] → [ 2 , 3 ] [2, 2, 3, 3] \rightarrow [2, 3] [2,2,3,3][2,3].

The final array has a length of 2 2 2, so the answer is 2 2 2. It can be proven that Sho cannot obtain an array with a longer length.

For the second test case Sho can perform operations as follows:

  1. Choose indices 3 3 3 and 4 4 4 to remove. The array becomes [ 9 , 1 , 9 , 9 , 1 ] → [ 9 , 1 , 1 ] [9, 1, 9, 9, 1] \rightarrow [9, 1, 1] [9,1,9,9,1][9,1,1].
  2. Choose indices 1 1 1 and 3 3 3 to remove. The array becomes [ 9 , 1 , 1 ] → [ 1 ] [9, 1, 1] \rightarrow [1] [9,1,1][1].

The final array has a length of 1 1 1, so the answer is 1 1 1. It can be proven that Sho cannot obtain an array with a longer length.

Tutorial
将多余的数的个数记录下来,最后将多余的数两两相消,如果最后还剩一个,只能舍弃一个单独的

Solution

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

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

void solve() {
    int n; 
    cin >> n;
    map<int, int> mp;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        mp[a[i]]++;
    }
    int cnt = 0;
    for (auto [_, y] : mp) {
        cnt += y - 1;
    }
    cout << n - (cnt & 1 ? cnt + 1 : cnt) << endl;
}

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

C. Where’s the Bishop?

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

Mihai has an 8 × 8 8 \times 8 8×8 chessboard whose rows are numbered from 1 1 1 to 8 8 8 from top to bottom and whose columns are numbered from 1 1 1 to 8 8 8 from left to right.

Mihai has placed exactly one bishop on the chessboard. The bishop is not placed on the edges of the board. (In other words, the row and column of the bishop are between 2 2 2 and 7 7 7, inclusive.)

The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked.

An example of a bishop on a chessboard. The squares it attacks are marked in red.

Mihai has marked all squares the bishop attacks, but forgot where the bishop was! Help Mihai find the position of the bishop.

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 36 1 \leq t \leq 36 1t36) — the number of test cases. The description of test cases follows. There is an empty line before each test case.

Each test case consists of 8 8 8 lines, each containing 8 8 8 characters. Each of these characters is either ‘#’ or ‘.’, denoting a square under attack and a square not under attack, respectively.

Output

For each test case, output two integers r r r and c c c ( 2 ≤ r , c ≤ 7 2 \leq r, c \leq 7 2r,c7) — the row and column of the bishop.

The input is generated in such a way that there is always exactly one possible location of the bishop that is not on the edge of the board.

Example
input

3

.....#..
#...#...
.#.#....
..#.....
.#.#....
#...#...
.....#..
......#.

#.#.....
.#......
#.#.....
...#....
....#...
.....#..
......#.
.......#

.#.....#
..#...#.
...#.#..
....#...
...#.#..
..#...#.
.#.....#
#.......

output

4 3
2 2
4 5

Note

The first test case is pictured in the statement. Since the bishop lies in the intersection row 4 4 4 and column 3 3 3, the correct output is 4 3.

Tutorial
直接暴力判断四个角和自己是否可以攻击到

Solution

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

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

void solve() {
    string g[8];
    for (string &gi : g) {
        cin >> gi;
    }
    for (int i = 1; i < 7; ++i) {
        for (int j = 1; j < 7; ++j) {
            if (g[i][j] == '#' && g[i - 1][j - 1] == '#' && g[i - 1][j + 1] == '#' && g[i + 1][j - 1] == '#' && g[i + 1][j + 1] == '#') {
                cout << i + 1 << " " << j + 1 << endl;
                return;
            }
        }
    }
}

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

D. The Clock

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

Victor has a 24-hour clock that shows the time in the format “HH:MM” (00 ≤ \le HH ≤ \le 23, 00 ≤ \le MM ≤ \le 59). He looks at the clock every x x x minutes, and the clock is currently showing time s s s.

How many different palindromes will Victor see in total after looking at the clock every x x x minutes, the first time being at time s s s?

For example, if the clock starts out as 03:12 and Victor looks at the clock every 360 360 360 minutes (i.e. every 6 6 6 hours), then he will see the times 03:12, 09:12, 15:12, 21:12, 03:12, and the times will continue to repeat. Here the time 21:12 is the only palindrome he will ever see, so the answer is 1 1 1.

A palindrome is a string that reads the same backward as forward. For example, the times 12:21, 05:50, 11:11 are palindromes but 13:13, 22:10, 02:22 are not.

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 description of each test case follows.

The only line of each test case contains a string s s s of length 5 5 5 with the format “HH:MM” where “HH” is from “00” to “23” and “MM” is from “00” to “59” (both “HH” and “MM” have exactly two digits) and an integer x x x ( 1 ≤ x ≤ 1440 1 \leq x \leq 1440 1x1440) — the number of minutes Victor takes to look again at the clock.

Output

For each test case, output a single integer — the number of different palindromes Victor will see if he looks at the clock every x x x minutes starting from time s s s.

Example
input

6
03:12 360
00:00 1
13:22 2
15:15 10
11:11 1440
22:30 27

output

1
16
10
0
1
1

Tutorial
从初始状态开始暴力模拟,当间隔的时间刚好是多少天时结束判断

Solution

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

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

void solve() {
    int x, ans = 0;
    string s;
    cin >> s >> x;
    int start = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3, 2)), times = 0;
    while (!times || times % 1440 != 0) {
        int now_h = (start + times) % 1440 / 60, now_m = (start + times) % 1440 % 60;
        if (now_h / 10 == now_m % 10 && now_h % 10 == now_m / 10) {
            ans++;
        }
        times += x;
    }
    cout << ans << endl;
}

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

E. Binary Deque

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

Slavic has an array of length n n n consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array.

What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to s s s after performing all the operations? In case the sum s s s can’t be obtained after any amount of operations, you should output -1.

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 s s s ( 1 ≤ n , s ≤ 2 ⋅ 1 0 5 1 \leq n, s \leq 2 \cdot 10^5 1n,s2105) — the length of the array and the needed sum of elements.

The second line of each test case contains n n n integers a i a_i ai ( 0 ≤ a i ≤ 1 0 \leq a_i \leq 1 0ai1) — the elements of the array.

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

Output

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to s s s, or -1 if obtaining an array with sum s s s isn’t possible.

Example
input

7
3 1
1 0 0
3 1
1 1 0
9 3
0 1 0 1 1 1 0 0 1
6 4
1 1 1 1 1 1
5 1
0 0 1 1 0
16 2
1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1
6 3
1 0 1 0 0 0

output

0
1
3
2
2
7
-1

Note

In the first test case, the sum of the whole array is 1 1 1 from the beginning, so we don’t have to make any operations.

In the second test case, the sum of the array is 2 2 2 and we want it to be equal to 1 1 1, so we should remove the first element. The array turns into [ 1 , 0 ] [1, 0] [1,0], which has a sum equal to 1 1 1.

In the third test case, the sum of the array is 5 5 5 and we need it to be 3 3 3. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into [ 0 , 1 , 1 , 1 , 0 , 0 ] [0, 1, 1, 1, 0, 0] [0,1,1,1,0,0], which has a sum equal to 3 3 3.

Tutorial
通过固定长度的滑动窗口,直接从左到右判断满足条件的最小值

Solution

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

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

void solve() {
    int n, s;
    cin >> n >> s;
    vector<int> a(n), idx;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i]) {
            idx.emplace_back(i);
        }
    }
    if (idx.size() < s) {
        cout << -1 << endl;
        return;
    }
    idx.emplace_back(n);
    int ans = n - idx[s];
    for (int i = 1; i < idx.size() - s; ++i) {
        ans = min(ans, n - (idx[i + s] - idx[i - 1] - 1));
    }
    cout << ans << endl;
}

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

F. 3SUM

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

Given an array a a a of positive integers with length n n n, determine if there exist three distinct indices i i i, j j j, k k k such that a i + a j + a k a_i + a_j + a_k ai+aj+ak ends in the digit 3 3 3.

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 ( 3 ≤ n ≤ 2 ⋅ 1 0 5 3 \leq n \leq 2 \cdot 10^5 3n2105) — 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 ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the elements of the array.

The sum of n n n across all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

Output t t t lines, each of which contains the answer to the corresponding test case. Output “YES” if there exist three distinct indices i i i, j j j, k k k satisfying the constraints in the statement, 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
4
20 22 19 84
4
1 11 1 2022
4
1100 1100 1100 1111
5
12 34 56 78 90
4
1 9 8 4
6
16 38 94 25 18 99

output

YES
YES
NO
NO
YES
YES

Note

In the first test case, you can select i = 1 i=1 i=1, j = 4 j=4 j=4, k = 3 k=3 k=3. Then a 1 + a 4 + a 3 = 20 + 84 + 19 = 123 a_1 + a_4 + a_3 = 20 + 84 + 19 = 123 a1+a4+a3=20+84+19=123, which ends in the digit 3 3 3.

In the second test case, you can select i = 1 i=1 i=1, j = 2 j=2 j=2, k = 3 k=3 k=3. Then a 1 + a 2 + a 3 = 1 + 11 + 1 = 13 a_1 + a_2 + a_3 = 1 + 11 + 1 = 13 a1+a2+a3=1+11+1=13, which ends in the digit 3 3 3.

In the third test case, it can be proven that no such i i i, j j j, k k k exist. Note that i = 4 i=4 i=4, j = 4 j=4 j=4, k = 4 k=4 k=4 is not a valid solution, since although a 4 + a 4 + a 4 = 1111 + 1111 + 1111 = 3333 a_4 + a_4 + a_4 = 1111 + 1111 + 1111 = 3333 a4+a4+a4=1111+1111+1111=3333, which ends in the digit 3 3 3, the indices need to be distinct.

In the fourth test case, it can be proven that no such i i i, j j j, k k k exist.

In the fifth test case, you can select i = 4 i=4 i=4, j = 3 j=3 j=3, k = 1 k=1 k=1. Then a 4 + a 3 + a 1 = 4 + 8 + 1 = 13 a_4 + a_3 + a_1 = 4 + 8 + 1 = 13 a4+a3+a1=4+8+1=13, which ends in the digit 3 3 3.

In the sixth test case, you can select i = 1 i=1 i=1, j = 2 j=2 j=2, k = 6 k=6 k=6. Then a 1 + a 2 + a 6 = 16 + 38 + 99 = 153 a_1 + a_2 + a_6 = 16 + 38 + 99 = 153 a1+a2+a6=16+38+99=153, which ends in the digit 3 3 3.

Tutorial
由于只需要判断三个数之和,所以每个数取余后保留三个同样的值就可以,所以一共可能存在 0 ~ 9 0 ~9 09 一共 10 10 10 种数,每种树最多存三个,所以最多只有 30 30 30 个数,最后直接三个 f o r for for 循环暴力求三个数的和,时间复杂度为 n + m i n ( n , 30 ) 3 n+min(n,30)^3 n+min(n,30)3

Solution

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

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

void solve() {
    int n, x;
    cin >> n;
    vector<int> a;
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        x %= 10;
        if (mp[x]++ < 3) {
            a.emplace_back(x);
        }
    }
    for (int i = 0; i < a.size(); ++i) {
        for (int j = i + 1; j < a.size(); ++j) {
            for (int k = j + 1; k < a.size(); ++k) {
                if ((a[i] + a[j] + a[k]) % 10 == 3) {
                    cout << "YES\n";
                    return;
                }
            }
        }
    }
    cout << "NO\n";
}

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

G. 2^Sort

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

Given an array a a a of length n n n and an integer k k k, find the number of indices 1 ≤ i ≤ n − k 1 \leq i \leq n - k 1ink such that the subarray [ a i , … , a i + k ] [a_i, \dots, a_{i+k}] [ai,,ai+k] with length k + 1 k+1 k+1 (not with length k k k) has the following property:

  • If you multiply the first element by 2 0 2^0 20, the second element by 2 1 2^1 21, …, and the ( k + 1 k+1 k+1)-st element by 2 k 2^k 2k, then this subarray is sorted in strictly increasing order.

More formally, count the number of indices 1 ≤ i ≤ n − k 1 \leq i \leq n - k 1ink such that
2 0 ⋅ a i < 2 1 ⋅ a i + 1 < 2 2 ⋅ a i + 2 < ⋯ < 2 k ⋅ a i + k . 2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}. 20ai<21ai+1<22ai+2<<2kai+k.

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 two integers n n n, k k k ( 3 ≤ n ≤ 2 ⋅ 1 0 5 3 \leq n \leq 2 \cdot 10^5 3n2105, 1 ≤ k < n 1 \leq k < n 1k<n) — the length of the array and the number of inequalities.

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 ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the elements of the array.

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 indices satisfying the condition in the statement.

Example
input

6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12

output

2
3
2
3
1
0

Note

In the first test case, both subarrays satisfy the condition:

  • i = 1 i=1 i=1: the subarray [ a 1 , a 2 , a 3 ] = [ 20 , 22 , 19 ] [a_1,a_2,a_3] = [20,22,19] [a1,a2,a3]=[20,22,19], and 1 ⋅ 20 < 2 ⋅ 22 < 4 ⋅ 19 1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19 120<222<419.
  • i = 2 i=2 i=2: the subarray [ a 2 , a 3 , a 4 ] = [ 22 , 19 , 84 ] [a_2,a_3,a_4] = [22,19,84] [a2,a3,a4]=[22,19,84], and 1 ⋅ 22 < 2 ⋅ 19 < 4 ⋅ 84 1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84 122<219<484.

In the second test case, three subarrays satisfy the condition:

  • i = 1 i=1 i=1: the subarray [ a 1 , a 2 ] = [ 9 , 5 ] [a_1,a_2] = [9,5] [a1,a2]=[9,5], and 1 ⋅ 9 < 2 ⋅ 5 1 \cdot 9 < 2 \cdot 5 19<25.
  • i = 2 i=2 i=2: the subarray [ a 2 , a 3 ] = [ 5 , 3 ] [a_2,a_3] = [5,3] [a2,a3]=[5,3], and 1 ⋅ 5 < 2 ⋅ 3 1 \cdot 5 < 2 \cdot 3 15<23.
  • i = 3 i=3 i=3: the subarray [ a 3 , a 4 ] = [ 3 , 2 ] [a_3,a_4] = [3,2] [a3,a4]=[3,2], and 1 ⋅ 3 < 2 ⋅ 2 1 \cdot 3 < 2 \cdot 2 13<22.
  • i = 4 i=4 i=4: the subarray [ a 4 , a 5 ] = [ 2 , 1 ] [a_4,a_5] = [2,1] [a4,a5]=[2,1], but 1 ⋅ 2 = 2 ⋅ 1 1 \cdot 2 = 2 \cdot 1 12=21, so this subarray doesn’t satisfy the condition.

Tutorial
从前往后计数:只要 a [ i − 1 ] < 2 ⋅ a [ i ] a[i - 1] < 2 \cdot a[i] a[i1]<2a[i] 就说明是满足条件的,就将计数器的值加一,如果此时计数器的值 c n t cnt cnt 满足 k ≤ c n t k \leq cnt kcnt,就说明以 i i i 为右端点的长度为 k k k 的区间是符合题意的

Solution

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

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

void solve() {
    int n, k, ans = 0, cnt = 1;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (i && a[i] * 2 > a[i - 1]) {
            cnt += 1;
            ans += (cnt > k);
        } else {
            cnt = 1;
        }
    }
    cout << ans << endl;
}

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

H. Gambling

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

Marian is at a casino. The game at the casino works like this.

Before each round, the player selects a number between 1 1 1 and 1 0 9 10^9 109. After that, a dice with 1 0 9 10^9 109 faces is rolled so that a random number between 1 1 1 and 1 0 9 10^9 109 appears. If the player guesses the number correctly their total money is doubled, else their total money is halved.

Marian predicted the future and knows all the numbers x 1 , x 2 , … , x n x_1, x_2, \dots, x_n x1,x2,,xn that the dice will show in the next n n n rounds.

He will pick three integers a a a, l l l and r r r ( l ≤ r l \leq r lr). He will play r − l + 1 r-l+1 rl+1 rounds (rounds between l l l and r r r inclusive). In each of these rounds, he will guess the same number a a a. At the start (before the round l l l) he has 1 1 1 dollar.

Marian asks you to determine the integers a a a, l l l and r r r ( 1 ≤ a ≤ 1 0 9 1 \leq a \leq 10^9 1a109, 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1lrn) such that he makes the most money at the end.

Note that during halving and multiplying there is no rounding and there are no precision errors. So, for example during a game, Marian could have money equal to 1 1024 \dfrac{1}{1024} 10241, 1 128 \dfrac{1}{128} 1281, 1 2 \dfrac{1}{2} 21, 1 1 1, 2 2 2, 4 4 4, etc. (any value of 2 t 2^t 2t, where t t t is an integer of any sign).

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 ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2\cdot 10^5 1n2105) — the number of rounds.

The second line of each test case contains n n n integers x 1 , x 2 , … , x n x_1, x_2, \dots, x_n x1,x2,,xn ( 1 ≤ x i ≤ 1 0 9 1 \leq x_i \leq 10^9 1xi109), where x i x_i xi is the number that will fall on the dice in the i i i-th round.

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.

Output

For each test case, output three integers a a a, l l l, and r r r such that Marian makes the most amount of money gambling with his strategy. If there are multiple answers, you may output any of them.

Example
input

4
5
4 4 3 4 4
5
11 1 11 1 11
1
1000000000
10
8 8 8 9 9 6 6 9 6 6

output

4 1 5
1 2 2
1000000000 1 1
6 6 10

Note

For the first test case, the best choice is a = 4 a=4 a=4, l = 1 l=1 l=1, r = 5 r=5 r=5, and the game would go as follows.

  • Marian starts with one dollar.
  • After the first round, he ends up with 2 2 2 dollars because the numbers coincide with the chosen one.
  • After the second round, he ends up with 4 4 4 dollars because the numbers coincide again.
  • After the third round, he ends up with 2 2 2 dollars because he guesses 4 4 4 even though 3 3 3 is the correct choice.
  • After the fourth round, he ends up with 4 4 4 dollars again.
  • In the final round, he ends up 8 8 8 dollars because he again guessed correctly.

There are many possible answers for the second test case, but it can be proven that Marian will not end up with more than 2 2 2 dollars, so any choice with l = r l = r l=r with the appropriate a a a is acceptable.

Tutorial
将数组内的值分类作前缀和,然后找最大值:用 m a p map map 将每个数出现的坐标存下来,转化为 1 1 1 − 1 -1 1 的模型。
对于每个数的坐标的处理,访问到的坐标是 1 1 1,其他的坐标均为 − 1 -1 1,利用前缀和判断 将每个数作为目标值最后所得的最多的钱
剩余细节见代码注释
感觉这个前缀和不能被模型思维束缚,要利用前缀和性质变通

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 + 1);
    map<int, vector<int>> mp;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        mp[a[i]].emplace_back(i); // 将每一个要猜的数进行分类
    }

    // 初始答案
    int ans = a[1], l = 1, r = 1, cnt = 1;
    for (auto [x, y] : mp) {
        // sum[i] 表示 y[i] 这个位置的前缀和
        vector<int> sum(y.size());
        sum[0] = 1;
        for (int i = 1; i < y.size(); ++i) {
            // 两个位置中间的部分都是别的数,用 -1 表示
            sum[i] = sum[i - 1] - (y[i] - y[i - 1] - 1) + 1;
        }
        int j = 0; // 用来保存左端点,i 用来保存右端点
        for (int i = 1; i < y.size(); ++i) {
            if (cnt < sum[i] - sum[j] + 1) { // 如果此时可以赚更多钱
                cnt = sum[i] - sum[j] + 1;
                l = y[j], r = y[i];
                ans = x;
            }
            if (sum[j] > sum[i]) { // 此时刷新左端点
                j = i;
            }
        }
    }
    cout << ans << " " << l << " " << r << endl;
}

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

写在后面

个人认为这次的 D 、 E 、 F 、 G 、 H D、E、F、G、H DEFGH 都非常有意思,都不是很难的题,但需要将题目要求模型化,再利用解决问题的方法性质去构造出解决题目的最佳方法,所需要的构造方法都非常巧妙,值得深推😊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值