D. Permutation Restoration

42 篇文章 0 订阅
10 篇文章 0 订阅

Monocarp had a permutation aa of nn integers 11, 22, ..., nn (a permutation is an array where each element from 11 to nn occurs exactly once).

Then Monocarp calculated an array of integers bb of size nn, where bi=⌊iai⌋bi=⌊iai⌋. For example, if the permutation aa is [2,1,4,3][2,1,4,3], then the array bb is equal to [⌊12⌋,⌊21⌋,⌊34⌋,⌊43⌋]=[0,2,0,1][⌊12⌋,⌊21⌋,⌊34⌋,⌊43⌋]=[0,2,0,1].

Unfortunately, the Monocarp has lost his permutation, so he wants to restore it. Your task is to find a permutation aa that corresponds to the given array bb. If there are multiple possible permutations, then print any of them. The tests are constructed in such a way that least one suitable permutation exists.

Input

The first line contains a single integer tt (1≤t≤1051≤t≤105) — number of test cases.

The first line of each test case contains a single integer nn (1≤n≤5⋅1051≤n≤5⋅105).

The second line contains nn integers b1,b2,…,bnb1,b2,…,bn (0≤bi≤n0≤bi≤n).

Additional constrains on the input:

  • the sum of nn over test cases does not exceed 5⋅1055⋅105;
  • there exists at least one permutation aa that would yield this array bb.

Output

For each test case, print nn integers — a permutation aa that corresponds to the given array bb. If there are multiple possible permutations, then print any of them.

Example

input

Copy

4
4
0 2 0 1
2
1 1
5
0 0 1 4 1
3
0 1 3

output

Copy

2 1 4 3 
1 2 
3 4 2 1 5 
3 2 1 

 思路:推柿子可以推出来a[i]的范围是【i / (b[i] + 1 ) ,i / b[i]】

然后我们就能推出来数组里每一个数的范围

我们从1开始选位置

找出1所在的范围之后,贪心取右边界最小的下标放这个1

例如当两个区间是(1,3)和(1,8)时,我们选(1,3)这个区间所在的下标放1

那么当下次的数是4的时候,我们就可以选(1,8)这个区间,即(1,8)能够给更多的数的选择的机会

当遇到右边界一样的情况的时候,我们任意选一个位置放就可以了。

www实现起来对于我这个菜鸡好复杂啊参考了别的dalao的代码。。。薄弱的stl知识让我回去重修语法课呜呜呜

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
	return b>0 ? gcd(b,a%b):a;
}
const int N=5e5+10;
int n;
struct name{
	int l,r,idx;
};
bool cmp(name a,name b){
	return a.l <b.l ;
}
int a[N];
name q[N];
void sove(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		if(x==0){
			q[i]={i+1,n,i};
		}else{
			int left=i/(x+1)+1;
			int right=i/x;
			q[i]={left,right,i};
		}
	}
	sort(q+1,q+1+n,cmp);
	priority_queue<pii,vector<pii>,greater<pii>> h;
	int num=0;
	for(int i=1;i<=n;i++){
		while(num+1<=n&&q[num+1].l ==i){
			h.push({q[++num].r ,q[num].idx }); 
		}
		a[h.top() .second]=i;
		h.pop();
	}
	for(int i=1;i<=n;i++)cout<<a[i]<<" ";
	cout<<endl;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
	cin>>t;
	while(t--){
		sove();
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: np.random.permutation 是 numpy 中的一个函数,它可以将一个数组中的元素随机打乱,返回一个打乱后的新数组。 使用方法如下: ```python import numpy as np # 对一个列表进行打乱 arr = [1, 2, 3, 4, 5] np.random.permutation(arr) # 对一个 numpy 数组进行打乱 arr = np.array([1, 2, 3, 4, 5]) np.random.permutation(arr) ``` 你也可以指定打乱的范围: ```python import numpy as np # 只打乱数组中的前 3 个元素 arr = np.array([1, 2, 3, 4, 5]) np.random.permutation(arr)[:3] ``` ### 回答2: np.random.permutation是NumPy库中的一个函数,用于对数组进行随机排列。它可以将数组中的元素打乱顺序,生成一个新的打乱后的数组。这个函数的使用非常灵活,可以接受多种不同类型的输入。 np.random.permutation函数可以接受一个整数作为参数,表示生成打乱顺序的数组的长度。比如,如果传入参数10,则会生成一个包含0到9的整数,且每个数字都会随机出现一次的数组。 此外,np.random.permutation函数还可以接受一个数组作为参数,表示对该数组进行随机排列。这个函数会返回一个新的打乱顺序后的数组,不会改变原始数组。这种用法可以用于在一些机器学习任务中,将数据集随机打乱,以便更好地进行训练。 需要注意的是,np.random.permutation函数对于数组的维度有特定的处理方式。如果传入的是一个n维数组,它会以第一个维度为准,对数组进行打乱顺序,而不会改变其他维度的相对位置。 总的来说,np.random.permutation函数是NumPy库中用于对数组进行随机排列的函数,可以生成一个新的打乱顺序的数组,或者对一个给定的数组进行随机排列。它的使用非常灵活,可以满足不同类型的需求。 ### 回答3: `np.random.permutation`是NumPy库中的一个函数,用于对给定的数组或整数序列进行随机排列。 当`np.random.permutation`的参数是一个整数n时,它会返回一个长度为n的随机排列的整数序列。例如,若`np.random.permutation(5)`返回[3, 0, 4, 1, 2]。 当`np.random.permutation`的参数是一个数组a时,它会返回一个随机排列的a的副本。该函数不会改变原始数组的顺序。例如,若`a = np.array([1, 2, 3, 4, 5])`,则`np.random.permutation(a)`可以返回[4, 1, 2, 5, 3]。 可以使用`np.random.permutation`函数来打乱数组的顺序,以便进行随机化处理或者洗牌操作。这对于在机器学习和数据分析中进行训练集与测试集划分、数据扩增等操作非常有用。 需要注意的是,`np.random.permutation`函数是基于随机数生成器的,通过设置`np.random.seed`函数可以获得重复的随机排列结果。此外,在高维数组中,`np.random.permutation`默认只会对第一个维度进行随机排序,若想对其他维度进行随机排序,可以使用`np.take`函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值