C. Serval and Toxel‘s Arrays

传送门

题目

给你一个长度为n的数组和m次修改,每次修改会将其中的一个数字修改成另一个数字然后形成一个新数组,初始数组和修改过程的数组保证没有相同的数字,问你所有任意两个数组之间去重之后元素的数量的和。

思路

考虑一下所有数组都不含相同元素的情况,那么就有(1+2+3+…+m-2+m-1+m)=m(m+1)/2对,然后每一对都有2n个,那么res结果为m(m+1)/2*2n。然后考虑如何去重就可以了。

在此我们可以假设一下,如果不改变所有元素,那么每个元素都会出现m+1次,对于每一次修改的元素x和被修改的y,如果接下来x不会被修改的话,那么x会增加m-i+1次(前提为所有数组都没有相同的数字),y会减少m-i+1次遍历一遍找每个元素重复的次数,然后用次数*(次数-1)/2得到每个元素重复所减少的贡献,然后用res减去这些所有的贡献就是最后的结果。

ps:(他奶奶的不会写这个看了题解也不懂,后来才发现每次修改后面的数字都会增加(如果不被修改),属于是题目又没理解到位了qwq)

代码

void slove( )
{
	int t;
	sc_int(n),sc_int(m);
	map<ll,ll>p;
	for(int i =1;i<=n;i++)
	{
		cin>>s[i];
		p[s[i]]=m+1;
	}
	ll res=0;
	for(int i=1;i<=m;i++)
	{
		int l,y;
		cin>>l>>y;
		int x=s[l];
		p[x]-=m-i+1;
		p[y]+=m-i+1;
		s[l]=y;
	}
	for(auto i:p){
		res+= i.second*(i.second-1)/2 ;
	}
	cout<<(ll)n*m*(m+1)-res;
	cout<<endl;
}
B. Serval and Final MEX time limit per test1 second memory limit per test256 megabytes You are given an array a consisting of n≥4 non-negative integers. You need to perform the following operation on a until its length becomes 1 : Select two indices l and r (1≤l<r≤|a| ), and replace the subarray [al,al+1,…,ar] with a single integer mex([al,al+1,…,ar]) , where mex(b) denotes the minimum excluded (MEX)∗ of the integers in b . In other words, let x=mex([al,al+1,…,ar]) , the array a will become [a1,a2,…,al−1,x,ar+1,ar+2,…,a|a|] . Note that the length of a decreases by (r−l) after this operation. Serval wants the final element in a to be 0 . Help him! More formally, you have to find a sequence of operations, such that after performing these operations in order, the length of a becomes 1 , and the final element in a is 0 . It can be shown that at least one valid operation sequence exists under the constraints of the problem, and the length of any valid operation sequence does not exceed n . Note that you do not need to minimize the number of operations. ∗ The minimum excluded (MEX) of a collection of integers b1,b2,…,bk is defined as the smallest non-negative integer x which does not occur in the collection b . Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows. The first line of each test case contains a single integer n (4≤n≤5000 ) — the length of the array a . The second line contains n integers a1,a2,…,an (0≤ai≤n ) — the elements of the array a . It is guaranteed that the sum of n over all test cases does not exceed 5000 . Output For each test case, output a single integer k (0≤k≤n ) in the first line of output — the length of the operation sequence. Then, output k lines, the i -th line containing two integers li and ri (1≤li<ri≤|a| ) — the two indices you choose in the i -th operation, where |a| denotes the length of the array be
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值