jiejiejiang2004

题解:Pinely Round 4 (Div. 1 + Div. 2) A

A. Maximize the Last Element

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given an array \(a\) of \(n\) integers, where \(n\) is odd.

In one operation, you will remove two adjacent elements from the array \(a\), and then concatenate the remaining parts of the array. For example, given the array \([4,7,4,2,9]\), we can obtain the arrays \([4,2,9]\) and \([4,7,9]\) by the operations \([\underline{4,7}, 4,2,9] \to [4,2,9]\) and \([4,7,\underline{4,2},9] \to [4,7,9]\) respectively. However, we cannot obtain the array \([7,2,9]\) as it requires deleting non-adjacent elements \([\underline{4},7,\underline{4},2,9]\).

You will repeatedly perform this operation until exactly one element remains in \(a\).

Find the maximum possible value of the remaining element in \(a\).

给你一个由 \(n\) 个整数组成的数组 \(a\) ,其中 \(n\)奇数

在一次操作中,你将从数组 \(a\) 中删除两个相邻的元素,然后将数组的剩余部分连接起来。例如,在数组 \([4,7,4,2,9]\) 中,我们可以通过操作 \([\underline{4,7}, 4,2,9] \to [4,2,9]\)\([4,7,\underline{4,2},9] \to [4,7,9]\) 分别得到数组 \([4,2,9]\)\([4,7,9]\) 。然而,我们无法得到数组 \([7,2,9]\) ,因为它需要删除不相邻的元素 \([\underline{4},7,\underline{4},2,9]\)

我们需要反复执行这个操作,直到 \(a\) 中只剩下一个元素。

\(a\) 中剩余元素的最大可能值。

Input

Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer \(n\) (\(1 \le n \le 99\); \(n\) is odd) — the length of the array \(a\).

The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) — the elements of the array \(a\).

Note that there is no bound on the sum of \(n\) over all test cases.

输入

每个测试包含多个测试用例。第一行包含一个整数 \(t\) ( \(1 \le t \le 1000\) ) - 测试用例数。测试用例说明如下。

每个测试用例的第一行包含一个整数 \(n\)\(1 \le n \le 99\)\(n\) 为奇数)--数组 \(a\) 的长度。

每个测试用例的第二行包含 \(n\) 个整数 \(a_1, a_2, \ldots, a_n\) ( \(1 \le a_i \le 100\) ) - 数组 \(a\) 的元素。

请注意,所有测试用例中 \(n\) 的总和不受约束。

Output

For each test case, output a single integer — the maximum possible value of the remaining element in \(a\).

输出

对于每个测试用例,输出一个整数 - \(a\) 中剩余元素的最大可能值。

Example

Input

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

Output

6
2
9
5

Note

In the first test case, the array \(a\) is \([6]\). Since there is only one element, no operations are needed. The maximum possible value of the remaining element is \(6\).

In the second test case, the array \(a\) is \([1, 3, 2]\). We can remove the first two elements \([\underline{1, 3}, 2] \to [2]\), or remove the last two elements \([1, \underline{3, 2}] \to [1]\). Therefore, the maximum possible value of the remaining element is \(2\).

In the third test case, the array \(a\) is \([4, 7, 4, 2, 9]\). One way to maximize the remaining element is \([4, \underline{7, 4}, 2, 9] \to [\underline{4, 2}, 9] \to [9]\). Therefore, the maximum possible value of the remaining element is \(9\).

In the fourth test case, the array \(a\) is \([3, 1, 4, 1, 5, 9, 2]\). It can be shown that the maximum possible value of the remaining element is \(5\).

在第一个测试用例中,数组 \(a\)\([6]\) 。由于只有一个元素,因此不需要进行任何操作。剩余元素的最大可能值为 \(6\)

在第二个测试案例中,数组 \(a\)\([1, 3, 2]\) 。我们可以删除前两个元素 \([\underline{1, 3}, 2] \to [2]\) 或删除最后两个元素 \([1, \underline{3, 2}] \to [1]\) 。因此,剩余元素的最大可能值为 \(2\)

在第三个测试案例中,数组 \(a\)\([4, 7, 4, 2, 9]\) 。最大化剩余元素的一种方法是 \([4, \underline{7, 4}, 2, 9] \to [\underline{4, 2}, 9] \to [9]\) 。因此,剩余元素的最大可能值是 \(9\)

在第四个测试案例中,数组 \(a\)\([3, 1, 4, 1, 5, 9, 2]\) 。可以证明剩余元素的最大可能值是 \(5\)

题意

执行无数次删去相邻两个元素,使得数组里仅剩一个元素(数组大小是奇数)
问:这个元素最大是多少

题解

删去相邻两个元素
这两个元素坐在的下标是一奇一偶
那么剩下元素下标的奇偶性不变
而最后只能剩下 \(1\) 个元素
说名剩下的元素所在的位置只能在奇数位置上
那我们只要比较出最大的奇数位置的元素即可

代码

#include <bits/stdc++.h>
#define int long long


void solve() {
    int n,num,max = -1e9;
    std::cin >> n;
    for(int i = 0 ; i < n ; i ++) {
        std::cin >> num;
        if(!(i&1)) max = std::max(max,num);
    }

    std::cout << max << "\n";
}

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

posted on 2024-07-30 19:40  Jiejiejiang  阅读(5)  评论(0编辑  收藏  举报

导航