7-2 Stack of Hats (25 分)
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 N N ( ≤ 1 0 4 ≤ 10^4 ≤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 1 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.
满分题解
/**
* 题目:PAT_A实战-映射-简单-2021秋-取帽子
*
* 说明:
* 1. 输入:整数n, 从底部到顶部n个帽子重量,n个人的重量wei[1,n], 每个人按顺序标号为[1,n]
* 2. 任务:如上所述
* 3. 输出:取帽子的人的顺序
*
* 技巧:
* 1. 看上去是栈的问题,其实不是(因为不涉及有变化的出入栈),只是映射问题
* 2. 映射的题目,变量命名很关键;好命名可以成倍地提高效率,坏命名会做成一团浆糊
*
*
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXN 10009
#define MAXSIZE 100009
#define MAXW 1000009
int n;
int hat[MAXN]; /* hat[i] <=> 第i帽子的重量;i = 0表示在最底端 */
int idh[MAXN]; /* idh[j] <=> 排名第j重的帽子的i */
vector<int> rnk_h; /* rnk_h[i] <=> 第i帽子的重量排名 */
int wei[MAXN]; /* wei[i] <=> 第i个人的重量 */
int idw[MAXN]; /* idw[j] <=> 排名第j重的人的i */
bool cmp_h(const int& a, const int& b)
{
return hat[a] < hat[b];
}
bool cmp_w(const int& a, const int& b)
{
return wei[a] < wei[b];
}
int main(void)
{
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> hat[i];
idh[i] = i;
rnk_h.push_back(0);
}
sort(idh, idh + n, cmp_h);
for (int i = 0; i < n; ++i) {
cin >> wei[i];
idw[i] = i;
}
sort(idw, idw + n, cmp_w);
for (int r = 0; r < n; ++r) {
rnk_h[idh[r]] = r;
}
int i = rnk_h.back(); /* 桥梁:帽子重量排名 <=> 拥有它的人的重量排名 */
cout << idw[i] + 1;
rnk_h.pop_back();
while (!rnk_h.empty()) {
i = rnk_h.back();
cout << " " << idw[i] + 1;
rnk_h.pop_back();
}
return 0;
}