CF2191A.Array Coloring
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have n cards arranged in a row. The i-th card has the integer ai written on it. All integers are distinct.
You must color each card either red or blue such that the following conditions are satisfied:
- Any two adjacent cards in the row have different colors.
- If you rearrange the cards so that the numbers on them are in increasing order, any two adjacent cards in the new row must also have different colors.
Determine if such a coloring exists.
你有 n 张卡片排成一行。第 i 张卡片上写有一个整数 ai。所有整数互不相同。
你需要将每张卡片染成 红色 或 蓝色,使得满足以下条件:
- 行中任意两张相邻的卡片颜色不同;
- 若将卡片按其上的数字升序重新排列,则新行中任意两张相邻的卡片颜色也必须不同。
判断是否存在满足上述条件的染色方案。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤200). The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤100) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n).
It is guaranteed that all elements of a are distinct.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤200)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤100)—— 表示数组的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)。
保证数组 a 中的所有元素互不相同。
输出格式
For each test case, output "YES" if you can color the cards so that the conditions are satisfied, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
对于每个测试用例,如果可以对卡片进行染色以满足所有条件,则输出 “YES”,否则输出 “NO”。
您可以以任意大小写形式输出答案(大写或小写)。例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均会被识别为肯定回答。
输入输出样例
输入#1
4 4 2 3 4 1 3 2 3 1 5 3 4 1 2 5 5 3 1 4 2 5
输出#1
YES NO YES NO
说明/提示
In the first example, the cards are colored as a=[2,3,4,1]. After sorting, the cards become [1,2,3,4]. Both sequences satisfy the condition, so the answer is YES.
In the second example, no valid coloring exists. For instance, if we color the cards as a=[2,3,1], the sorted sequence becomes [1,2,3]. Here, the adjacent elements 1 and 2 have the same color, so the condition is not satisfied.
In the third example, a possible coloring is a=[3,4,1,2,5]. After sorting, the cards become [1,2,3,4,5].
在第一个例子中,卡片的颜色为 a=[2,3,4,1]。排序后,卡片变为 [1,2,3,4]。两个序列均满足条件,因此答案为 YES。
在第二个例子中,不存在合法的染色方案。例如,若将卡片染色为 a=[2,3,1],则排序后的序列为 [1,2,3]。此时相邻元素 1 和 2 颜色相同,不满足条件。
在第三个例子中,一种可能的染色方案是 a=[3,4,1,2,5]。排序后,卡片变为 [1,2,3,4,5]。
输入解题思路,AI测评打分。不知道怎么写?