7-10 Stack of Hats (25 分)(C语言解决)

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.

大白话:就是一堆帽子从上到下的进行分配,大的帽子给体重大的人,同时反馈出来该人的下标。

因为大的要给大的,所以我们肯定需要进行比大小工作,然而,目前就我所学的,排序都是需要进行数据改造的,那么我们这个时候就需要新建一个数组,让他来存储相应的数据排序好的,考虑到我们既需要排名又需要该数据对应的位置,我们新定义的两个数组,在存储完鞋子的数据或者人的体重后,同时又有一个结构体变量存储他们本来的位置,本来想着帽子的号和人的体重都记录一下rank后来超时了,所以又进行了改造,我们只需要比较帽子的排名即可(此时帽子类型的数据都已经排序排好了),对人的体重也进行了排名,此时帽子号的排名也就是对应的按体重进行排序的人的序号,同时人的rank用来存储他们原来的下标(这里的rank是指person的rank。),最后输出下标即可。我也想了想如果不去排名帽子,让rank也是去统计他们原来的下标,但是这样出现的一个麻烦就是,我们需要找到下标对应的那个元素(经过排序后),也就是说我们需要进行一次遍历,但是这样无疑时又增加了时间复杂度,所以我们还是找一个数的rank,最终也是通过了。

上代码:

#include <stdio.h>
typedef struct{
    int hp;
    int rank;
}ss;
int main(){
    int N;
    scanf("%d",&N);
    ss s[N+1];
    ss p[N+1];
    ss o[N+1];
    ss q[N+1],temp;
    int i,j,n=0;
    for(i=1;i<N+1;i++)scanf("%d",&s[i].hp);
    for(i=1;i<N+1;i++)scanf("%d",&o[i].hp);
    for(i=1;i<N+1;i++){p[i]=s[i];p[i].rank=i;}
    for(i=1;i<N+1;i++){q[i]=o[i];q[i].rank=i;}
    for(i=1;i<N+1;i++){
        for(j=i+1;j<N+1;j++){//根据鞋子的大小进行排名
            if(p[i].hp<p[j].hp){
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=1;i<N+1;i++){//统计出来当前每个鞋所对应的鞋的排名
    	for(j=1;j<N+1;j++){
    		if(p[i].hp==s[j].hp){
    			s[j].rank=++n;
    			break;
			}
		}
	}
	    for(i=1;i<N+1;i++){//对每个人的体重进行排序
        for(j=i+1;j<N+1;j++){
            if(q[i].hp<q[j].hp){
                temp=q[i];
                q[i]=q[j];
                q[j]=temp;
            }
        }
    }
    n=0;
    for(i=N;i>0;i--){//person对应的结构体ss中的rank是记录下标作用,而hat里面的rank是真的rank也就是排名。
     			if(i!=1)printf("%d ",q[s[i].rank].rank);//打印出对应的格式。
     			else printf("%d\n",q[s[i].rank].rank);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值