A. Leha and Function
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Leha like all kinds of strange things. Recently he liked the function F(n, k). Consider all possible k-element subsets of the set [1, 2, …, n]. For subset find minimal element in it. F(n, k) — mathematical expectation of the minimal element among all k-element subsets.
But only function does not interest him. He wants to do interesting things with it. Mom brought him two arrays A and B, each consists of m integers. For all i, j such that 1 ≤ i, j ≤ m the condition Ai ≥ Bj holds. Help Leha rearrange the numbers in the array A so that the sum is maximally possible, where A’ is already rearranged array.
Input
First line of input data contains single integer m (1 ≤ m ≤ 2·105) — length of arrays A and B.
Next line contains m integers a1, a2, …, am (1 ≤ ai ≤ 109) — array A.
Next line contains m integers b1, b2, …, bm (1 ≤ bi ≤ 109) — array B.
Output
Output m integers a’1, a’2, …, a’m — array A’ which is permutation of the array A.
Examples
input
5
7 3 5 3 4
2 1 3 2 3
output
4 7 3 5 3
input
7
4 6 5 8 8 2 6
2 1 2 2 1 1 2
output
2 6 4 5 8 8 6
题意 : F(n,k) 表示从 1 ~ n 里选出 k 个数,每次选出的 k 个数的贡献为 k 个数里最小哪个数,把 a 数组重新拍下序,使得 F(ai,bi) 的和最大
思路 : 推导下会发现 F(n,k) = C(n,k) * 1 + C(n - 1,k) * 2….+C(n - (n - k),k) k , n 相同时 k 越小 ,F(n,k) 越大,让 较大 的 n 和 较小的 k 配对,最优
AC代码:
#include
#include
#include
#include
using namespace std;
const int MAX = 2e5 + 10;
typedef long long LL;
struct node{
int a,o;
}s[MAX],st[MAX];
bool cmp(node i,node j) { return i.a > j.a; }
bool cnp(node i,node j) { return i.a < j.a; }
bool cpp(node i,node j) { return i.o < j.o; }
int main()
{
int m; scanf("%d",&m);
for(int i = 0; i < m; i++) scanf("%d",&st[i].a),st[i].o = i;
for(int i = 0; i < m; i++) scanf("%d",&s[i].a),s[i].o = i;
sort(st,st + m,cmp),sort(s,s + m,cnp);
for(int i = 0; i < m; i++) st[i].o = s[i].o;
sort(st,st + m,cpp);
for(int i = 0; i < m; i++) printf("%d ",st[i].a);
return 0;
}