PATers believe that wearing hats makes them look handsome, so wherever they go, everyone of them would wear a hat. One day they came to a restaurant, a waiter collected their hats and piled them up. But then when they were ready to leave, they had to face a stack of hats as shown by the above figure. So your job is to help them line up so that everyone can pick up his/her hat one by one in order without any trouble.
It is known that every hat has a unique size, which is related to the weight of its owner – that is, the heavier one wears larger hat.\
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N ( ≤ 1 0 4 ) N (≤10^4) N(≤104) which is the number of PATers. The next line gives N N N distinct sizes of the hats, which are positive numbers no more than 1 0 5 10^5 105. The sizes correspond to the hats from bottom up on the stack. Finally in the last line, N N N distinct weights are given, correspond to the hat owners numbered from 1 to N N N. The weights are positive numbers no more than 1 0 6 10^6 106. All the numbers in a line are separated by spaces.
Output Specification:
For each test case, print in a line the indices of the hat owners in the order of picking up their hats. All the numbers must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.
Sample Input:
10
12 19 13 11 15 18 17 14 16 20
67 90 180 98 87 105 76 88 150 124
Sample Output:
3 4 8 6 10 2 1 5 9 7
Hint:
The 1st hat has the largest size 20, hence must correspond to the one with the largest weight, which is the 3rd one of weight 180. So No.3 must go first.
The 2nd hat has the 6th smallest size 16, hence corresponds to the 6th smallest weight, which is 98. So No.4 is the next to go.
And so on so forth.
Caution:
疯狂哈希!哈就完事了。
解释清楚输出的这一串就知道整个的思路了:
for(int i = n - 1; i >= 0; --i){
if(i == 0) printf("%d\n", weight2Pos[rank2Weight[hats2Rank[hats[i]]]]);
else printf("%d ", weight2Pos[rank2Weight[hats2Rank[hats[i]]]]);
}
hats[i]
是第 i 个帽子;
hats2Rank[hats[i]]
返回第 i 个帽子是排名第 x 大的;
rank2Weight[hats2Rank[hats[i]]]
返回第 x 重的体重是多少(记为 y);
weight2Pos[rank2Weight[hats2Rank[hats[i]]]]
返回重量是 y 的顾客所在的位置。
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-09-11 14:04:21
// All rights reserved.
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
int weights[10010];
int hats[10010], hats2[10010];
unordered_map<int, int> weight2Pos, rank2Weight;
unordered_map<int, int> hats2Rank;
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &hats[i]);
hats2[i] = hats[i];
}
sort(hats2, hats2 + n, greater<int>());
for(int i = 0; i < n; ++i) hats2Rank[hats2[i]] = i + 1;
for(int i = 0; i < n; ++i){
scanf("%d", &weights[i]);
weight2Pos[weights[i]] = i + 1;
}
sort(weights, weights + n, greater<int>());
for(int i = 0; i < n; ++i) rank2Weight[i + 1] = weights[i];
for(int i = n - 1; i >= 0; --i){
if(i == 0) printf("%d\n", weight2Pos[rank2Weight[hats2Rank[hats[i]]]]);
else printf("%d ", weight2Pos[rank2Weight[hats2Rank[hats[i]]]]);
}
return 0;
}