首页 > 其他分享 >题解:Pinely Round 4 (Div. 1 + Div. 2) A

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

时间:2024-07-30 19:39:02浏览次数:10  
标签:le 题解 array 元素 underline 数组 test Div Round

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;
}

标签:le,题解,array,元素,underline,数组,test,Div,Round
From: https://www.cnblogs.com/jiejiejiang2004/p/18333234

相关文章

  • P4449 于神之怒加强版 (题解)
    题目链接P4449于神之怒加强版题目大意:求\[\sum_{i=1}^{n}\sum_{j=1}^{m}gcd(i,j)^k\]\(数据范围n,m\leq5e6\)\(二话不说,先开导式子(假定n<m):\)\begin{aligned}ans&=\sum_{i=1}^{n}\sum_{j=1}^{m}gcd(i,j)^k\\\end{aligned}\[先套路地枚举d=gcd(i,j)\]\begin{align......
  • 题解 - Alice
    题目描述Alice和Bob在玩游戏,游戏规则如下:有两堆石子,每堆石子有非负整数个Alice与Bob轮流操作,Alice先手,每次可以从任意一堆石子中取出若干个取出的石子个数应满足为另一堆石子个数的因数(此处规定任何数均为0的因数)将石子取完的玩家获胜给定一个初始状态,现......
  • [POI2007] OSI-Axes of Symmetry 题解
    给出一个多边形,求对称轴数量。考虑对于一个多边形,其是对称的当且仅当对于若干个边(角),其左右的角与边都是对称的。考虑如果我们对于内角构造出一种单射,映射为一个整数,将边映射为它的边长,那么我们按照角,边,角,边,……的顺序将他们加入数组中,能构造出一个长度为\(2n\)的数组,将这个......
  • Codeforces Round 933 (Div. 3) D 题
    D.RudolfandtheBallGame原题链接:https://codeforces.com/contest/1941/problem/D RudolfandBernarddecidedtoplayagamewiththeirfriends. n peoplestandinacircleandstartthrowingaballtoeachother.Theyarenumberedfrom 1 to nn i......
  • Codeforces Round 929 (Div. 3)---->E. Turtle vs. Rabbit Race: Optimal Trainings
    https://codeforces.com/contest/1933/problem/E#include<bits/stdc++.h>#definexfirst#defineysecondusingnamespacestd;typedeflonglongll;typedef__int128i128;typedefpair<int,int>pii;constintN=2e5+10,M=110;intn,q;inta[N];ll......
  • 旅行 题解
    题目id:20300题目描述鱼大大计划环华夏自驾游游玩一圈,在他的计划中,这次旅行将在\(m\)天内完成,预计游玩\(n\)个城市,每个城市游玩\(x\)天,然后赶路开车以均速去到下一个城市,现鱼大大从海洋城市出发,按游玩顺序先后给出每个城市和上一个城市之间的距离(\(km\)),问鱼大大每天至少赶路多......
  • 质数差列 题解
    题目id:20315题目描述驰骋宇宙的鱼大大找到了一个古遗迹,稍作研究后发现这是一个来着远古的质数星球文明遗迹,这个文明的特点是所有事物都和质数息息相关。于是,鱼大大赶紧列出了一堆的质数,以方便自己的研究。这天鱼大大找到了质数星球文明的一个遗迹仓库大门,正准备破解密码的同时......
  • [POI2008] POC-Trains 题解
    前言题目链接:洛谷。时间复杂度和输入同阶的做法。题意简述有\(n\)(\(n\leq10^3\))个长\(m\)的字符串,\(q\)(\(q\leq10^5\))次操作,交换两个字符串的两个字符。问每个字符串在所有时刻,最多有几个和它相等。题目分析套路做法看到字符串相等,想到使用哈希。但是要支持修改,怎么......
  • 题解:[ABC363D] Palindromic Number
    提示特定位数的回文数数量是可以快速计算出来的,对于特定的位数\(n\),第一位由于不能有前导\(0\),共有\(9\)种选择,而从第\(2\)位到第\(\frac{n+1}{2}\)位都有\(10\)种选择(一个回文数完全由它的前半部分确定)。所以\(n\)位数中共有\(S(n)=9*10^\frac{n-1}{2}......
  • P3811 【模板】模意义下的乘法逆元 题解
    【模板】模意义下的乘法逆元题目背景这是一道模板题题目描述给定n,pn,pn,p求......