题解: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 a a of n n n integers, where n n n is odd.

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

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

Find the maximum possible value of the remaining element in a a a.

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

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

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

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

Input

Each test contains multiple test cases. 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 description of test cases follows.

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

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 100 1 \le a_i \le 100 1ai100) — the elements of the array a a a.

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

输入

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

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

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

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

Output

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

输出

对于每个测试用例,输出一个整数 - a 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 a a is [ 6 ] [6] [6]. Since there is only one element, no operations are needed. The maximum possible value of the remaining element is 6 6 6.

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

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

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

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

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

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

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

题意

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

题解

删去相邻两个元素
这两个元素坐在的下标是一奇一偶
那么剩下元素下标的奇偶性不变
而最后只能剩下 1 1 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;
}

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18333234
博主已同意,我就是博主

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值