CF2250A.Threshold Movement

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are n+2n+2 positions numbered from 00 to n+1n+1. Initially, position ii contains an element of weight wiw_i for every 1≤i≤n1\le i\le n, while positions 00 and n+1n+1 are empty.

You choose an integer kk. Then every element moves exactly once, simultaneously:

  • If wi<kw_i \lt k, the element at position ii moves to position i−1i-1;
  • If wi>kw_i \gt k, the element at position ii moves to position i+1i+1;
  • If wi=kw_i=k, the entire movement process fails immediately.

An integer kk is perfect if the movement does not fail and, upon completion, every position from 11 to nn contains exactly one element.

Determine whether a perfect integer kk exists.

共有 n+2n+2 个位置,编号从 00 到 n+1n+1。初始时,对每个 1≤i≤n1\le i\le n,位置 ii 上有一个权重为 wiw_i 的元素;而位置 00 和 n+1n+1 为空。

你选择一个整数 kk。随后,所有元素同时且恰好移动一次:

  • 若 wi<kw_i \lt k,则位于位置 ii 的元素移动到位置 i−1i-1;
  • 若 wi>kw_i \gt k,则位于位置 ii 的元素移动到位置 i+1i+1;
  • 若 wi=kw_i=k,则整个移动过程立即失败。

若整数 kk 满足移动过程不失败,且移动完成后,位置 11 至 nn 上恰好各有一个元素,则称 kk 为完美整数。

判断是否存在完美整数 kk。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains one integer nn (1≤n≤1001\le n\le 100).

The second line of each test case contains nn integers w1,w2,…,wnw_1,w_2,\ldots,w_n (1≤wi≤1091\le w_i\le 10^9).

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001\le n\le 100)。

每个测试用例的第二行包含 nn 个整数 w1,w2,…,wnw_1,w_2,\ldots,w_n(1≤wi≤1091\le w_i\le 10^9)。

输出格式

For each test case, print "YES" if a perfect integer kk exists, 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.

对于每个测试用例,如果存在完美整数 kk,则输出 "YES";否则输出 "NO"。

你可以以任意大小写形式输出答案(大写或小写)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。

输入输出样例

  • 输入#1

    6
    1
    7
    2
    3 1
    2
    2 1
    4
    9 1 7 2
    4
    9 8 7 1
    6
    1000000000 1 9 2 8 3

    输出#1

    NO
    YES
    NO
    YES
    NO
    YES

说明/提示

In the first test case, the only element either leaves position 11 or has weight equal to kk, so no suitable integer exists.

In the second test case, choose k=2k=2. The element of weight 33 moves right and the element of weight 11 moves left, leaving one element in each position.

In the third test case, keeping both positions occupied would require 1<k<21 \lt k \lt 2, which is impossible for an integer kk.

In the fourth test case, k=5k=5 is suitable: the elements at positions 11 and 33 move right, while those at positions 22 and 44 move left. Upon completion, every position from 11 to 44 contains exactly one element.

In the fifth test case, the element at position 22 must move left, requiring k>8k \gt 8, while the element at position 33 must move right, requiring k<7k \lt 7. These requirements are incompatible.

In the sixth test case, choose k=4k=4. All elements at odd positions move right and all elements at even positions move left, so every position from 11 to 66 contains one element afterwards.

在第一个测试用例中,唯一的元素要么离开位置 11,要么其权重等于 kk,因此不存在满足条件的整数。

在第二个测试用例中,选择 k=2k=2。权重为 33 的元素向右移动,权重为 11 的元素向左移动,使得每个位置恰好剩下一个元素。

在第三个测试用例中,若要使两个位置均被占据,则需满足 1<k<21 \lt k \lt 2,但不存在满足该不等式的整数 kk。

在第四个测试用例中,k=5k=5 是可行的:位置 11 和 33 上的元素向右移动,位置 22 和 44 上的元素向左移动。操作完成后,位置 11 至 44 每个位置恰好包含一个元素。

在第五个测试用例中,位置 22 上的元素必须向左移动,要求 k>8k \gt 8;而位置 33 上的元素必须向右移动,要求 k<7k \lt 7。这两项要求相互矛盾。

在第六个测试用例中,选择 k=4k=4。所有位于奇数位置的元素向右移动,所有位于偶数位置的元素向左移动,因此操作完成后,位置 11 至 66 每个位置恰好包含一个元素。

输入解题思路,AI测评打分。不知道怎么写?

首页