Knowledge Test about Match(思维)

题目描述

It’s classical to match two arrays to minimize the loss function, such as Network flow and Kuhn-Munkres algorithm. Here comes a small test.

You are given two sequences a , b {a,b} a,b with length n {n} n.

a = { 0 , 1 , 2 , … , n − 2 , n − 1 } , b = { b 0 , b 1 , b 2 , … , b n − 2 , b n − 1 } a=\{0,1,2,\dots,n-2,n-1\},b=\{b_0,b_1,b_2,\dots,b_{n-2},b_{n-1}\} a={0,1,2,,n2,n1},b={b0,b1,b2,,bn2,bn1}.

Now you are allowed to rearrage sequence b b b arbitrarily. Formally, you can assign a permutation p {p} p between 0 {0} 0 to n − 1 {n-1} n1 and do the following transformation: b i ′ = b p i b'_i=b_{p_i} bi=bpi.

Finally you should minimize the loss function f ( a , b ) = ∑ i = 0 n − 1 ∣ a i − b i ∣ f(a,b)=\sum \limits_{i=0}^{n-1} \sqrt{|a_i-b_i|} f(a,b)=i=0n1aibi .

Because the time limit is too strict to solve, you don’t need to find the exact value of f ( a , b ) {f(a,b)} f(a,b). Denote the minimal value is f k ∗ f_k^{\ast} fk and your result is f ^ k \hat f_k f^k (where k {k} k means the k {k} k-th test case), you will be accepted if : 1 T ∑ k = 1 T f k ^ − f k ∗ f k ∗ ≤ 4 % \frac{1}{T}\sum \limits_{k=1}^T \frac{\hat {f_k} - f_k^{\ast}}{f_k^{\ast}} \le 4\% T1k=1Tfkfk^fk4%.

输入描述:

There are multiple test cases. The first line of the input contains an integer T ( 100 ≤ T ≤ 500 ) T(100 \le T \le 500) T(100T500), indicating the number of test cases. For each test case:

The first line contains an integer n ( 10 ≤ n ≤ 1000 ) n(10 \le n \le 1000) n(10n1000) indicating the length of sequences. The second line contains n n n integers, describing the sequence b b b.

It’s guaranteed that the sum of n n n over all test cases does not exceed 4 × 1 0 4 4 \times 10^4 4×104.

Notice: For each i ∈ [ 0 … n − 1 ] i \in [0 \dots n-1] i[0n1], b i b_i bi is selected from [ 0 … n − 1 ] [0 \dots n-1] [0n1] independently with equal probability. But the length of arrays in each test case can be assigned manually.

输出描述:

For each test case, output n {n} n integers in a line, indicating the sequence b {b} b after rearranging.

示例1

输入

2
10
4 8 9 9 6 1 3 9 5 2
54
0 0 0 1 2 2 4 4 5 6 7 10 16 17 18 19 19 20 23 24 25 26 26 26 27 29 30 30 31 32
32 33 34 36 37 37 38 39 41 42 42 43 43 44 44 45 45 46 50 50 51 53 53 53

输出

9 1 2 3 4 5 6 9 8 9
0 1 2 2 4 5 6 7 4 0 10 0 53 43 32 30 16 17 18 19 20 19 26 23 24 25 26 27 26 29
30 31 32 33 34 37 36 37 38 39 42 41 42 43 44 45 46 45 44 50 50 51 53 53

说明

The sample test doesn’t obey 100 ≤ T ≤ 500 100 \le T \le 500 100T500 but the final tests obey.

For the first test case, the output reaches the optimal value. But for the second test case, the output is not optimal and the approximate ratio is 3.82 % 3.82\% 3.82%.

So the average approximate ratio of the sample test is 1.91 % 1.91\% 1.91% without exceeding 4 % 4\% 4%.

思路

举个例子, a = { 0 , 1 , 2 } a=\{0,1,2\} a={0,1,2},若 b = { 1 , 2 , 3 } b=\{1,2,3\} b={1,2,3},那么 f ( a , b ) = 3 f(a,b)=3 f(a,b)=3,若 b = { 3 , 1 , 2 } b=\{3,1,2\} b={3,1,2},那么 f ( a , b ) = 3 f(a,b)=\sqrt3 f(a,b)=3 。所以,这题不能直接 s o r t sort sort

考虑到 F ( x ) = x F(x)=\sqrt x F(x)=x 的图像,斜率递减。所以这题的大致思路方向为:允许有相差特别大的两个点,但要尽可能使相差小的点更少。

所以先处理相差为 0 0 0 的点,再处理相差为 1 1 1 的点,以此类推。

这题不要求最优解,所以可以使用贪心方法。

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int T,n,v,a[N],ans[N];

int main(){
	for(scanf("%d",&T);T;T--){
		scanf("%d",&n);
		for(int i=0;i<1000;i++)
			a[i]=ans[i]=0;
		for(int i=0;i<n;i++)
			scanf("%d",&v), a[v]++, ans[v]=v;
		for(int k=1;k<n;k++)
			for(int i=0;i<n;i++){
				if(a[i]>1&&i-k>=0&&ans[i-k]==0)
					ans[i-k]=i, a[i-k]++, a[i]--;	
				if(a[i]>1&&i+k<n&&ans[i+k]==0)
					ans[i+k]=i, a[i+k]++, a[i]--;	
			}
		for(int i=0;i<n;i++)
			printf("%d%c",ans[i]," \n"[i==n-1]);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

m0_51864047

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

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

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

打赏作者

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

抵扣说明:

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

余额充值