Codeforces Round #835 (Div. 4) E. Binary Inversions

A sequence of nn numbers is called a permutation if it contains all integers from 11 to nn exactly once. For example, the sequences [3,1,4,23,1,4,2], [11] and [2,12,1] are permutations, but [1,2,11,2,1], [0,10,1] and [1,3,41,3,4] — are not.

Polycarp lost his favorite permutation and found only some of its elements — the numbers b1,b2,…bmb1,b2,…bm. He is sure that the sum of the lost elements equals ss.

Determine whether one or more numbers can be appended to the given sequence b1,b2,…bmb1,b2,…bm such that the sum of the added numbers equals ss, and the resulting new array is a permutation?

Input

The first line of input contains a single integer tt (1≤t≤1001≤t≤100) —the number of test cases.

Then the descriptions of the test cases follow.

The first line of each test set contains two integers mm and ss (1≤m≤501≤m≤50, 1≤s≤10001≤s≤1000)—-the number of found elements and the sum of forgotten numbers.

The second line of each test set contains mm different integers b1,b2…bmb1,b2…bm (1≤bi≤501≤bi≤50) — the elements Polycarp managed to find.

Output

Print tt lines, each of which is the answer to the corresponding test set. Print as the answer YES if you can append several elements to the array bb, that their sum equals ss and the result will be a permutation. Output NO otherwise.

You can output the answer in any case (for example, yEs, yes, Yes and YES will be recognized as positive answer).

Note

For the first test case, the inversions are initially formed by the pairs of indices (1,21,2), (1,41,4), (3,43,4), being a total of 33, which already is the maximum possible.

For the second test case, the inversions are initially formed by the pairs of indices (2,32,3), (2,42,4), (2,62,6), (5,65,6), being a total of four. But, by flipping the first element, the array becomes 1,1,0,0,1,01,1,0,0,1,0, which has the inversions formed by the pairs of indices (1,31,3), (1,41,4), (1,61,6), (2,32,3), (2,42,4), (2,62,6), (5,65,6) which total to 77 inversions which is the maximum possible.

 

很明显这个题需要求3种数列的值,并且输出其中的量最大值,第一个是将原数列第一个0翻转为1,第二个将原数列最后一个1翻转为0,第三个是原数列;我们需要对以上三个数列分别进行处理求出其中的量的多少并且输出量最大的即可;

其中需要明白的是'!'符号的作用,!符号作用翻转比如!a,如果a=0,那么!a=1,同理如果a=1,那么!a=0;

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int e[200100];
int w[200100];
int n;
long long check(int temp)
{
	int num=0;
	e[temp]=!e[temp];//翻转对应值 
	long long ans=0;
	for(int i=n;i>=1;i--)//因为记录的是该数字以后的0的个数所以要从后向前进行遍历 
	{
		num+=!e[i];//由于是记录0的个数所以+翻转后的值 
		if(e[i]==1)//当出现一的时候就代表要将ans将之前判断的 0值储存进来 
		{
			ans+=num;
		}
	}
	e[temp]=!e[temp];//在该次判断记录完毕后应将其重新翻转回去否则会影响下次判断 
	return ans; 
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;//n个数字被存入 
		for(int i=1;i<=n;i++)
		{
			cin>>e[i];
		}
		int temp1=0;//记录其中最后一个1所处原数组的位置 
		int temp0=0;//记录其中第一个0所处原数组的位置
		for(int i=1;i<=n;i++)//利用循环对其进行遍历找出相应位置
		{
			if(e[i]==0)
			{
				temp0=i;//找到后就退出 
				break;
			}	
		}
		for(int i=n;i>=1;i--)//利用循环对其进行遍历找出相应位置
		{
			if(e[i]==1)
			{
				temp1=i;
				break;
			}	
		}
		
		cout<<max({check(temp1),check(temp0),check(0)})<<endl;//输出三个值里的最大值 
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tang_7777777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值