17.看楼房

Description

小张在暑假时间进行了暑期社会调查。调查的内容是楼房的颜色如何影响人们的心情。于是他找到了一个楼房从左到右排成一排的小区,这个小区一共有 n 栋楼房,每个楼房有一个颜色 ci 和一个高度 hi 。小张调查的内容为每次他站在第 i 栋楼和第 i+1 栋楼之间向左看,他记录下此时他看到的楼房颜色数作为他的调查结果。

由于小张在暑假时间沉迷游戏来不及做实地调查,只好拜托你将调查结果告诉他。

Input

本题有多组数据。

每组数据第一行一个整数 n 。表示有 n 栋楼房从左到右排成一排。

第二行 n 个数,表示每个楼房的颜色

(1 \leq c_i \leq 10^6 )

第三行 n 个数,表示每个楼房的高度

(1 \leq c_i \leq 10^9 )

数据保证所有组数据的

\sum{n} \leq 1000000

Output

每组数据输出 n 个数,第 i 个数表示他站在第 i 栋楼和第 i+1 栋楼之间向左看,能够看到的楼房颜色数。

Notes

在从左向右看楼房的时候,左边较矮的楼房会被右边较高的楼房挡住。

测试输入期待的输出时间限制内存限制额外进程
测试用例 1以文本方式显示
  1. 2↵
  2. 5↵
  3. 1 2 3 4 5↵
  4. 1 3 4 2 5↵
  5. 5↵
  6. 1 2 4 4 5↵
  7. 1 3 4 2 5↵
以文本方式显示
  1. 1 1 1 2 1↵
  2. 1 1 1 1 1↵
1秒64M0
测试用例 5以文本方式显示
  1. 1↵
  2. 1000000↵
  3. 210894 163116 160080 147818 149341 182487 47691 125431 166628 66786 126057 200700 137717 98602 140619 92537 102889 156231 68679 114080 157904 93696 38769 7804 129770 45661 204057 37615 856 103262 93884 140584 198480 23847 17513 14915 38494 29791 177935 39755 184152 136545 93142 197906 15027 162566 183049 28091 156593 123729 150865 199551 104819 34482 158665 54827 124961 26337 61727 14651 9188 163144 43688 107614 129606 2310 211872 194589 186685 21098 202160 200098 12478 109407 11159 87094 185644 35895 196518 189298 192129 165236 45550 78827 209013 113906 134385 111098 49910 9668 151568 78011 136402 170946 36058 196974 147552 139978 121334 151780 162817 135327 121725 83773 132784 22940 8280 69937 138789 1158 35948 117150 3791 211172 35357 93901 120335 209961 12180 152105 70044 101248 30397 34496 179404 56621 126541 132172 75359 200615 98020 146517 113303 37415 11599 97790 93061 26565 73803 106871 27597 5012 89464 159927 79212 187429 171423 194322 36322 160011 215349 59136 131780 61954 115304 113900↵
以文本方式显示
  1. 1 1 2 3 2 3 4 3 2 3 3 4 3 4 5 4 5 3 4 2 3 4 5 6 4 5 5 6 5 5 6 7 8 8 9 10 10 6 2 3 3 4 3 3 4 5 5 5 6 5 6 7 7 7 8 7 8 8 9 8 9 8 8 5 6 7 7 8 9 9 1 2 2 3 3 3 4 4 3 4 5 6 5 4 5 2 3 2 3 3 3 3 1 2 2 3 4 4 3 3 4 4 5 6 7 5 5 6 6 6 7 8 7 5 6 6 6 7 5 6 7 7 8 9 4 5 2 3 3 4 3 4 4 5 6 6 3 4 4 5 5 5 5 6 6 6 6 7 8 4 5 5 6 6 7 5 6 6 6 7 8 9 6 7 1 2 3 3 4 4 2 3 4 3 4 5 6 5 6 2 3 4 4 4 5 4 5 6 6 5 6 5 6 5 5 6 6 6 6 7 2 3 3 3 4 3 4 2 3 3 4 5 3 4 4 3 4 5 6 7 8 8 3 4 5 4 5 5 4 5 5 4 5 6 6 5 6 2 3 2 3 4 5 5 3 1 2 2 3 4 4 1 2 3 4 4 5 2 3 4 2 3 3 2 3 4 5 2 3 3 4 4 5 6 5 5 3 4 4 4 4 5 5 4 5 5 5 6 6 7 7 6 7 4 5 5 6 6 2 3 4 3 4 4 5 6 7 5 5 6 6 7 8 9 3 4 5 6 4 5 3 4 5 6 6 7 8 9 5 6 6 7 7 6 7 8 6 7 7 8 8 8 9 8 5 6 6 6 7 7 8 8 9 6 7 7 8 8 8 8 8 9 9 9 10 11 9 10 10 11 11 3 4 4 4 5 6 6 6 6 5 6 6 7 5 6 5 4 5 5 6 7 8 7 6 6 7 8 5 6 7 8 4 5 5 6 5 2 3 4 3 4 4 3 3 4 4 5 4 4 4 5 6 6 4 5 5 5 3 4 5 6 4 3 4 4 5 4 3 4 5 6 4 5 5 5 5 5 5 4 5 6 3 4 5 4 4 4 5 6 6 5 6 7 7 6 7 8 9 7 8 9 8 8 9 9 7 8 9 9 9 9 10 9 10 8 9 6 7 7 8 8 7 8 8 8 8 9 9 10 11 10 9 10 9 ↵
1秒64M0
思路

三个数组height,color和count。count用来统计颜色。
对于第i个房子,如果栈空直接进栈就好。如果比栈顶矮且该颜色count0,入栈,同时ans++,count=1;如果count非0则没有进栈必要。如果比栈顶高,则弹出栈顶,同时栈顶颜色count0,ans--;直到比栈顶矮。最后输出ans。
注意每组数据初始化,尤其是count,top和ans。

代码 
//
#include<stdio.h>
#include<string.h>
#define N 1000002

int temp[N]={0},height[N]={0},color[N]={0},count[N]={0},i,h,top,ans;

int push()
{
	if(top==N-1)	return 0;
	if(count[temp[i]])	return 2;
	height[++top]=h;
	count[temp[i]]++;
	color[top]=temp[i];
	ans++;
	return 1;
}

int pop()
{
	if(top==-1)	return 0;
	count[color[top]]--;
	ans--;
	top--;
	return 1;
}
main()
{
	int t,n,j;
	scanf("%d",&t);
	while(t--){
		top=-1;
		ans=0;
		memset(count,0,N);
		
		scanf("%d",&n);
		for(j=0;j<n;j++)	scanf("%d",&temp[j]);
		
		for(i=0;i<n;i++){
			scanf("%d",&h);
			while(top>-1){
				if(h>=height[top])	pop();
				else	break;
			}		
			push();
			
			printf("%d",ans);
			if(i==n-1)	printf("\n");
			else	printf(" ");		
		}	
	}
}
/*
2
5
1 2 3 4 5
1 3 4 2 5
5
1 2 4 4 5
1 3 4 2 5
*/

  • 28
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的楼房销售系统的 Python 代码: ```python class House: def __init__(self, address, price, area, rooms): self.address = address self.price = price self.area = area self.rooms = rooms class Salesman: def __init__(self, name, commission): self.name = name self.commission = commission def sell_house(self, house, buyer): if buyer.budget >= house.price: buyer.budget -= house.price self.commission += house.price * 0.1 print("Congratulations, " + buyer.name + "! You have successfully purchased " + house.address + " for " + str(house.price) + " dollars.") else: print("Sorry, " + buyer.name + "! You cannot afford " + house.address + ".") class Buyer: def __init__(self, name, budget): self.name = name self.budget = budget salesman = Salesman("John", 0) house1 = House("123 Main St", 500000, 2000, 4) house2 = House("456 Elm St", 300000, 1500, 3) house3 = House("789 Oak St", 700000, 2500, 5) buyer1 = Buyer("Alice", 600000) buyer2 = Buyer("Bob", 400000) salesman.sell_house(house1, buyer1) # Congratulations, Alice! You have successfully purchased 123 Main St for 500000 dollars. salesman.sell_house(house2, buyer2) # Sorry, Bob! You cannot afford 456 Elm St. salesman.sell_house(house3, buyer2) # Congratulations, Bob! You have successfully purchased 789 Oak St for 700000 dollars. ``` 这个系统包含三个类:House、Salesman 和 Buyer。House 类表示一栋房子,包含地址、价格、面积和房间数等属性。Salesman 类表示销售员,包含名字和佣金等属性,以及一个 sell_house 方法,用于销售房子。Buyer 类表示买家,包含名字和预算等属性。在主程序中,创建了三个 House 对象和两个 Buyer 对象,然后调用 Salesman 的 sell_house 方法进行销售。如果买家的预算足够支付房子的价格,销售员会计算佣金并打印一条祝贺消息;否则会打印一条失败消息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北辰2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值