每日一题 第八十期 CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)

本文介绍了一个编程问题,要求根据给定的数组a计算每个位置的MEX值,并基于这些MEX值构造满足条件的排列p。Bessie需要找到至少一种可能的排列p,使得a[i]=MEX(p[1],p[2],...,p[i])-p[i]。
摘要由CSDN通过智能技术生成

B. Bessie and MEX

time limit per test: 1.5 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

MOOO! - Doja Cat

Farmer John has a permutation p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn, where every integer from 0 0 0 to n − 1 n-1 n1 occurs exactly once. He gives Bessie an array a a a of length n n n and challenges her to construct p p p based on a a a.

The array a a a is constructed so that a i a_i ai = MEX ( p 1 , p 2 , … , p i ) − p i \texttt{MEX}(p_1, p_2, \ldots, p_i) - p_i MEX(p1,p2,,pi)pi, where the MEX \texttt{MEX} MEX of an array is the minimum non-negative integer that does not appear in that array. For example, MEX ( 1 , 2 , 3 ) = 0 \texttt{MEX}(1, 2, 3) = 0 MEX(1,2,3)=0 and MEX ( 3 , 1 , 0 ) = 2 \texttt{MEX}(3, 1, 0) = 2 MEX(3,1,0)=2.

Help Bessie construct any valid permutation p p p that satisfies a a a. The input is given in such a way that at least one valid p p p exists. If there are multiple possible p p p, it is enough to print one of them.
Input

The first line contains t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the lengths of p p p and a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( − n ≤ a i ≤ n -n \leq a_i \leq n nain) — the elements of array a a a.

It is guaranteed that there is at least one valid p p p for the given data.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.
Output

For each test case, output n n n integers on a new line, the elements of p p p.

If there are multiple solutions, print any of them.

Example
inputCopy
3
5
1 1 -2 1 2
5
1 1 1 1 1
3
-2 1 2
outputCopy
0 1 4 2 3
0 1 2 3 4
2 0 1

Note

In the first case, p = [ 0 , 1 , 4 , 2 , 3 ] p = [0, 1, 4, 2, 3] p=[0,1,4,2,3] is one possible output.

a a a will then be calculated as a 1 = MEX ( 0 ) − 0 = 1 a_1 = \texttt{MEX}(0) - 0 = 1 a1=MEX(0)0=1, a 2 = MEX ( 0 , 1 ) − 1 = 1 a_2 = \texttt{MEX}(0, 1) - 1 = 1 a2=MEX(0,1)1=1, a 3 = MEX ( 0 , 1 , 4 ) − 4 = − 2 a_3 = \texttt{MEX}(0, 1, 4) - 4 = -2 a3=MEX(0,1,4)4=2, a 4 = MEX ( 0 , 1 , 4 , 2 ) − 2 = 1 a_4 = \texttt{MEX}(0, 1, 4, 2) - 2 = 1 a4=MEX(0,1,4,2)2=1, a 5 = MEX ( 0 , 1 , 4 , 2 , 3 ) − 3 = 2 a_5 = \texttt{MEX}(0, 1, 4, 2, 3) - 3 = 2 a5=MEX(0,1,4,2,3)3=2.

So, as required, a a a will be [ 1 , 1 , − 2 , 1 , 2 ] [1, 1, -2, 1, 2] [1,1,2,1,2].

AC代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;

int t;
int n;
int h[N];
int a[N];
int main()
{
	cin >> t;
	while(t --){
		cin >> n;
		memset(h, 0, sizeof h);
		int flag = n;
		for(int i = 1; i <= n; i ++)
		{
			cin >> a[i];
		}
		for(int i = n; i >= 1; i --)
		{
			int x = flag - a[i];
			if(flag > x) flag = x;
			h[i] = x;
		}
		for(int i = 1; i <= n; i ++)
		{
			cout << h[i] << " ";
		}
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值