Consecutive Points Segment

Consecutive Points Segment

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given nn points with integer coordinates on a coordinate axis OXOX. The coordinate of the ii-th point is xixi. All points' coordinates are distinct and given in strictly increasing order.

For each point ii, you can do the following operation no more than once: take this point and move it by 11 to the left or to the right (i..e., you can change its coordinate xixi to xi−1xi−1 or to xi+1xi+1). In other words, for each point, you choose (separately) its new coordinate. For the ii-th point, it can be either xi−1xi−1, xixi or xi+1xi+1.

Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer ll the coordinates of points should be equal to l,l+1,…,l+n−1l,l+1,…,l+n−1.

Note that the resulting points should have distinct coordinates.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of points in the set xx.

The second line of the test case contains nn integers x1<x2<…<xnx1<x2<…<xn (1≤xi≤1061≤xi≤106), where xixi is the coordinate of the ii-th point.

It is guaranteed that the points are given in strictly increasing order (this also means that all coordinates are distinct). It is also guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each test case, print the answer — if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO.

Example

input

5
2
1 4
3
1 2 3
4
1 2 3 7
1
1000000
3
2 5 6

output

YES
YES
NO
YES
YES
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    int t, n;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 0;i < n ;i++)
            cin >> a[i];
        int k = n - 2;//中间的数字的个数
        int ans = a[n - 1] - a[0] - 2 - 1;//比较两边的距离和中间数字的个数
        //如1 2 3 7    1,7变为2,6中间如果有3(6-2-1)个数的话就为YES
        if(ans <= k)
            cout << "YES" <<endl;
        else
            cout << "NO" << endl;

    }
    return 0;
}

 

07-23
Sure! Here's an example C++ code that solves the problem: ```cpp #include <iostream> #include <vector> #include <unordered_set> int findMinimumSizeOfDoubleRainbow(const std::vector<int>& colors, int k) { int n = colors.size(); std::vector<int> colorCount(k + 1, 0); std::unordered_set<int> leftColors; int minSize = n + 1; // Iterate over all possible sizes of P' from 1 to n for (int size = 1; size <= n; size++) { // Reset color counts and left colors for each size std::fill(colorCount.begin(), colorCount.end(), 0); leftColors.clear(); // Count colors in the first size elements for (int i = 0; i < size; i++) { colorCount[colors[i]]++; leftColors.insert(colors[i]); } bool foundDoubleRainbow = true; // Check if there exists a valid P' of size 'size' for (int i = 0; i < n - size + 1; i++) { // Check if all colors are present in both P' and P \ P' if (leftColors.size() == k && colorCount == std::vector<int>(k + 1, 1)) { minSize = std::min(minSize, size); foundDoubleRainbow = true; break; } // Update color counts and left colors colorCount[colors[i]]--; if (colorCount[colors[i]] == 0) { leftColors.erase(colors[i]); } colorCount[colors[i + size]]++; leftColors.insert(colors[i + size]); } if (!foundDoubleRainbow) { break; } } return (minSize == n + 1) ? 0 : minSize; } int main() { int n, k; std::cin >> n >> k; std::vector<int> colors(n); for (int i = 0; i < n; i++) { std::cin >> colors[i]; } int minSize = findMinimumSizeOfDoubleRainbow(colors, k); std::cout << minSize << std::endl; return 0; } ``` You can compile and run this code to solve the problem. It reads the input from standard input and prints the minimum size of 𝑃 ′ that makes a double rainbow to standard output. Note: This code assumes that the input is valid and follows the given constraints. You may need to add additional input validation if necessary.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值