CF2231B.Another Sorting Problem
普及-
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a1,a2,…,an. You can perform the following operation on this array at most once:
- Choose a positive integer k, as well as a subsequence b1,b2,…,bm of the array a∗ and add k to each element of this subsequence, that is, for each i perform abi:=abi+k.
You need to determine whether it is possible to sort the array in nondecreasing order by performing this operation at most once.
∗A sequence b is a subsequence of a sequence a if b can be obtained from a by the deletion of several (possibly, zero or all) element from arbitrary positions.
给你一个数组 a1,a2,…,an。你最多可以对这个数组执行以下操作一次:
- 选择一个正整数 k,以及数组 a 的一个子序列 b1,b2,…,bm∗,并将 k 加到该子序列的每个元素上,即对每个 i 执行 abi:=abi+k。
你需要判断:是否可以通过最多执行一次该操作,将数组排序为非递减顺序。
∗ 序列 b 是序列 a 的一个子序列,当且仅当 b 可通过从 a 中任意位置删除若干(可能为零个或全部)元素而得到。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
In the first line of each test case, one integer n is given (1≤n≤2⋅105) — the length of the array a.
In the second line of each test case, n integers a1,a2,…,an are given (1≤ai≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
在每个测试用例的第一行中,给出一个整数 n(1≤n≤2⋅105)—— 表示数组 a 的长度。
在每个测试用例的第二行中,给出 n 个整数 a1,a2,…,an(1≤ai≤109)。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print "Yes" if it is possible to sort the array by performing the operation at most once. Otherwise, print "No". You may print the answer in any case. For example, "YeS", "YES", "NO", "nO" will also be accepted.
对于每个测试用例,如果可以通过至多执行一次该操作将数组排序,则输出“Yes”;否则输出“No”。你可以以任意大小写形式输出答案。例如,“YeS”、“YES”、“NO”、“nO”均被接受。
输入输出样例
输入#1
8 4 1 2 3 4 5 10 5 9 4 4 7 8 3 9 4 10 5 11 6 5 3 10 5 9 7 3 2 1 1 5 2 1 1 2 1 3 3 1 2 5 2 1 2 3 1
输出#1
YES NO YES NO YES YES YES NO
说明/提示
In the first test case, the array is already sorted, so the operation does not need to be performed at all.
In the second test case, it can be shown that it is impossible to sort the array using such an operation.
In the third test case, choose k=6 and choose the subsequence [2,4,6] as b. Then after this operation, the array a becomes [8,9,9,10,10,11,11] (the elements of the chosen subsequence are shown in bold).
在第一个测试用例中,数组已经有序,因此完全不需要执行该操作。
在第二个测试用例中,可以证明无法通过此类操作将数组排序。
在第三个测试用例中,选择 k=6,并选择子序列 [2,4,6] 作为 b。执行该操作后,数组 a 变为 [8,9,9,10,10,11,11](所选子序列的元素以粗体显示)。
输入解题思路,AI测评打分。不知道怎么写?