首页 > 其他分享 >Constructive Problem

Constructive Problem

时间:2023-06-30 19:46:00浏览次数:37  
标签:operation int MEX test Problem Constructive cases array

Constructive Problem time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

As you know, any problem that does not require the use of complex data structures is considered constructive. You are offered to solve one of such problems.

You are given an array a of n non-negative integers. You are allowed to perform the following operation exactly once: choose some non-empty subsegment al,al+1,…,ar,+1,…, of the array a and a non-negative integer k, and assign value k to all elements of the array on the chosen subsegment.

The task is to find out whether MEX(a)MEX⁡() can be increased by exactly one by performing such an operation. In other words, if before the operation MEX(a)=mMEX⁡()= held, then after the operation it must hold that MEX(a)=m+1MEX⁡()=+1.

Recall that MEXMEX of a set of integers c1,c2,…,ck1,2,…, is defined as the smallest non-negative integer x which does not occur in the set c.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500001≤≤50000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2000001≤≤200000) — the number of elements of array a.

The second line of each test case contains n integers a1,a2,…,an1,2,…, (0≤ai≤1090≤≤109) — elements of array a.

It is guaranteed that the sum n over all test cases does not exceed 200000200000.

Output

For each test case, print "Yes" if you can increase MEX(a)MEX⁡() by exactly one by performing the operation from the statement exactly once, otherwise print "No".

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.

Example input Copy 4 3 1 2 1 4 0 2 2 0 4 3 2 0 2 1 0 output Copy
Yes
Yes
No
No
Note

In the first test case, MEX(a)=0MEX⁡()=0. If you set all elements of a to 00, then MEXMEX of the resulting array will be 11, and thus will increase by one.

In the second test case, MEX(a)=1MEX⁡()=1. If we assign a value of 11 to the elements of a on a subsegment from 22 to 33, we get an array [0,1,1,0][0,1,1,0] for which MEXMEX is 22, and thus is increased by one compared to the original.

It can be shown that in the third and fourth test cases it is impossible to perform an operation so that the value of MEX(a)MEX⁡() increases by exactly one.

 
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e5+10,mod=1e9+7;
string s;
int n,t,a[N],f[N],res,num,ans,m;
bool vis[N];
int mex()//寻找mex
{
    unordered_map<int,int>g;
    for(int i=0;i<n;i++) g[a[i]]=1;
    for(int i=0;i<=n;i++)
        if(!g.count(i)) return i;
}
int main()
{
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=0;i<n;i++) cin>>a[i];
        num=mex();
        int l=-1,r=-1;
        for(int i=0;i<n;i++){//这一步是记录下来数组里k+1的左端点和右端点
            if(a[i]==num+1){
                if(l==-1) l=i;
                r=i;
            }
        }
        if(l==-1&&r==-1){//如果数组里面没有k+1,那么要判断
        //如果k>=n的话就不行,例如 0 1 2 3 ,k=4,那就不行,因为数不够,否则就可以
            if(num<n) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
            continue;
        }
        else{
            for(int i=l;i<=r;i++) a[i]=num;//改完之后再判断一次
            ans=mex();
            if(ans==num+1) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}

 

标签:operation,int,MEX,test,Problem,Constructive,cases,array
From: https://www.cnblogs.com/o-Sakurajimamai-o/p/17517682.html

相关文章

  • constructive algorithms
    E.MishaandPaintingshttps://codeforces.com/problemset/problem/1720/E题意:给到一个n*n矩阵,问至少需要几次操作才能使得矩阵中有exactlyk个点。每次操作定义为选定一个方阵,将其所有元素变为x,x自定义。n<=500,k<=n2,aij<=n2题解:对于这类构造题,我们往往希望粗调逼近所需值......
  • 【每日一题】Problem 489B. BerSU Ball
    原题解决思路排序+双指针#include<bits/stdc++.h>intmain(){intn;std::cin>>n;std::vector<int>a(n+1,0);for(inti=1;i<=n;++i)std::cin>>a[i];intm;std::cin>>m;std::vector<int>b(m+1,0);......
  • 【每日一题】Problem 443B. Kuriyama Mirai's Stones
    原题解决思路两个数组,一个未排序,一个排序;使用前缀和的方式减少时间复杂度#include<bits/stdc++.h>intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);intn;std::cin>>n;std::vector<int>v(n+1,0);f......
  • 【cs50 2022】lab1 && problem set1
    |lab1|#include<cs50.h>#include<stdio.h>intmain(void){//TODO:Promptforstartsizeintstart_size;do{start_size=get_int("Startsize:");}while(start_size<9);//TODO:Promptforend......
  • 【每日一题】Problem 359B. Permutation
    原题解决思路虽然题解思路里写着\(dp\),但是还是用最简单的\(math\)方法写了找规律题:如果结果为\(0\),只需要每个\(a_{2i-1}-a_{i}\)同向即可,即都为整数或负数如果结果为\(2k\),则只需要任意一个\(|a_{2i-1}-a_{i}|\)的值为\(k\),且与其他的\(a_{2i-1}-a_{i}\)方向......
  • 【每日一题】Problem 253B. Physics Practical
    原题解决思路定义\(dp[i][j]\)为对\(i\)元素做出选择后,要删除的最少元素个数对于\(i\),有两种情况,选或不选选:则找到\(y(y>2x)\)的个数,可以通过排序二分实现不选:则在\(i-1\)的最少删除个数的选择下\(+1\)#include<bits/stdc++.h>intbinarySearch(std::vect......
  • 【每日一题】Problem 234C. Weather
    原题解决思路还是先从二维数组开始,定义一个二维数组\(dp\),对于\(dp[i,j]\),\(i\)表示第\(i\)个元素,\(j\)表示当前元素的状态(正数或负数)\(dp[i,0]\)表示第\(i\)个元素为负数时,前\(i\)个元素中需要改动的元素数;\(dp[i,1]\)表示第\(i\)个元素为正数时,前\(i\)个......
  • 每日一题力扣 1262 https://leetcode.cn/problems/greatest-sum-divisible-by-three/
    、 题解这道题目核心就算是要知道如果x%3=2的话,应该要去拿%3=1的数字,这样子才能满足%3=0贪心sum不够%3的时候,就减去余数为1的或者余数为2的需要注意两个余数为1会变成余数为2的,所以可能减去2个余数为1核心代码如下publicintmaxSumDivThreeOther(int[]nums){​  ......
  • Yet Another Minimization Problem(CF1637D)
    \(\text{Des}\)Youaregiventwoarrays$a$and$b$,bothoflength$n$.Youcanperformthefollowingoperationanynumberoftimes(possiblyzero):selectanindex$i$($1\leqi\leqn$)andswap$a_i$and$b_i$.Let'sdefi......
  • 登陆服务器异常ABRT has detected 1 problem(s).
    1、登陆服务器后,出现如下所示错误:ABRThasdetected1problem(s).Formoreinforun:abrt-clilist--since16862382592、执行提示命令[root@hadoop1~]#abrt-clilist--since16862382593、启用自动报告功能[root@hadoop1~]#abrt-auto-reportingenabled4、重新链接测试,没......