[贪心]Permutation Restoration CF1701D

22 篇文章 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

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

output

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

题意: 给出一个长度为n的数组b,求一个全排列a,对于每个位置都满足bi = floor(i/ai),floor为下取整函数。

分析: 根据bi = floor(i/ai)可以求出每个ai的取值范围,由于i/ai-1 < floor(i/ai) <= i/ai,所以从上式得到i/(bi+1) < ai <= i/bi,ai是个整数,那么有floor(i/(bi+1))+1 <= ai <= floor(i/bi),确定了ai的取值范围后问题就变成了从每个区间中选一个数来构成全排列,这个问题可以用贪心的思想来解决,首先对于区间右端点排序,这是为了防止出现取了区间[a, b+1]中的b后再取[c, b]中b+1的情况,如果二者交换顺序就没问题了,然后每个区间选之前未出现的最小的数。但是如果直接这样交的话会TLE,在枚举区间内数字时可以加上一个优化,先维护出来一个pos值,该值表示1~pos-1全部已选,然后枚举时每次从max(pos, l[i])开始枚举到r[i],为了充分发挥这个优化能提升的性能,在区间排序时,如果右端点相同则按左端点升序排序,这样就能AC了。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int b[500005];
bool vis[500005];
struct node{
	int l, r, pos;
}p[500005];

bool cmp(node x, node y){
	if(x.r != y.r) return x.r < y.r;
	return x.l < y.l;
}

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++){
			scanf("%d", &b[i]);
			p[i].pos = i;
			if(b[i] == 0){
				p[i].l = i+1;
				p[i].r = n;
			}
			else{
				p[i].l = i/(b[i]+1)+1;
				p[i].r = i/b[i];
			}
			vis[i] = false;
		}
		sort(p+1, p+1+n, cmp);
		int pos = 1;
		for(int i = 1; i <= n; i++){
			while(vis[pos]) pos++;
			for(int j = max(p[i].l, pos); j <= p[i].r; j++){
				if(!vis[j]){
					vis[j] = true;
					b[p[i].pos] = j;
					break;
				}
			}
		}
		for(int i = 1; i <= n; i++)
			printf("%d ", b[i]);
		puts("");
	}
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值