CF2233D.Goods on the Shelf
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In a supermarket, goods of the same type are usually placed next to each other so that the shelf looks neat and it is easier for customers to find what they need.
The shelf is described by an array a of n elements, where ai is the type of the good at position i.
We will say that the shelf is arranged correctly if for every two positions i and j such that 1≤i<j≤n and ai=aj, the following condition holds: for each k from i to j, it is true that ak=ai. In other words, goods of each type on the shelf must form one contiguous block.
You are allowed to choose two different positions at most once and swap the goods at these positions. You may also choose not to perform any swap.
Determine whether it is possible to make the shelf arranged correctly after that.
在超市中,相同类型的商品通常被放置在相邻的位置,以使货架看起来整齐,并方便顾客找到所需的商品。
货架由一个包含 n 个元素的数组 a 描述,其中 ai 表示位置 i 处商品的类型。
我们称货架排列正确,当且仅当对任意两个满足 1≤i<j≤n 且 ai=aj 的位置 i 和 j,以下条件成立:对每个从 i 到 j 的 k,均有 ak=ai。换言之,货架上每种类型的商品必须构成一个连续的块。
你最多可以执行一次操作:选择两个不同的位置,并交换这两个位置上的商品。你也可以选择不执行任何交换。
请判断:是否可以通过上述操作(或不操作)使货架排列正确。
输入格式
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.
The first line of each test case contains an integer n (2≤n≤2⋅105) — the number of goods on the shelf.
The second line of each test case contains n integers ai (1≤ai≤109), where ai denotes the type of the good at position i.
Additional input constraints:
- the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 表示货架上商品的数量。
每个测试用例的第二行包含 n 个整数 ai(1≤ai≤109),其中 ai 表示位置 i 处商品的类型。
附加输入约束:
- 所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, output one of the following:
- NO, if it is impossible to arrange the shelf correctly;
- YES, if it is possible to make the shelf arranged correctly with at most one swap of two goods.
You may output the answer in any case. For example, "YeS", "YES", "NO", "nO" will also be accepted.
对于每个测试用例,输出以下之一:
NO,如果无法正确排列货架;YES,如果最多交换两个商品即可使货架正确排列。
您可以以任意大小写形式输出答案。例如,YeS、YES、NO、nO 均可被接受。
输入输出样例
输入#1
7 3 1 2 1 2 7 7 6 1 2 3 1 2 3 6 1 1 2 3 2 3 7 1 2 3 1 2 3 4 6 1 2 1 2 1 1 6 1 2 2 3 3 1
输出#1
YES YES NO YES NO YES NO
说明/提示
In the first example, you can swap the goods at positions 1 and 2, after which the shelf will look like this: [2,1,1].
In the second example, the shelf is already arranged correctly.
In the third example, it is impossible to arrange the shelf correctly with one swap.
In the sixth example, you can swap the goods at positions 1 and 4, after which the shelf will be arranged correctly.
在第一个例子中,你可以交换位置 1 和 2 处的商品,交换后货架将变为:[2,1,1]。
在第二个例子中,货架已经正确排列。
在第三个例子中,仅通过一次交换无法使货架正确排列。
在第六个例子中,你可以交换位置 1 和 4 处的商品,交换后货架将正确排列。
输入解题思路,AI测评打分。不知道怎么写?