7-2 Stack of Hats

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 (≤104) which is the number of PATers. The next line gives N distinct sizes of the hats, which are positive numbers no more than 105. The sizes correspond to the hats from bottom up on the stack. Finally in the last line, N distinct weights are given, correspond to the hat owners numbered from 1 to N. The weights are positive numbers no more than 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.

#include<iostream>
#include<stack>
#include<unordered_map>
#include<algorithm>
using namespace std;
const int N = 10010;
int n;
stack<int> stk;
unordered_map<int,int> order_hap,order_weight,pos;
int w[N],s[N];
int main(){
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int x;scanf("%d",&x);
        stk.push(x);
        s[i] = x;
        order_hap[s[i]] = i;
    }
    sort(s,s+n,greater<int>());
    for(int i = 0;i<n;i++){
        order_hap[s[i]] = i+1;
    }
    for(int i = 0;i<n;i++){
        scanf("%d",&w[i]);
        order_weight[w[i]] = i+1;
    }
    sort(w,w+n,greater<int>());
    for(int i = 0;i<n;i++){
        pos[i+1] = w[i];
    }
    while(stk.size()){
        int hap = stk.top();
        stk.pop();
        int hap_r = order_hap[hap];
        if(stk.size()) printf("%d ",order_weight[pos[hap_r]]);
        else printf("%d",order_weight[pos[hap_r]]);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值