2018.05.19 A:贝壳找房性价比





思想:

联系所学数学知识,要求的式子的意义可以把p看成纵坐标,把s看成横坐标,然后求两点连线斜率的最大值。

在多个点中找斜率最大的两个点的直线

若进行二重循环用两个for会超时,想别的方法,先将各个点按横坐标进行排序,然后相邻两点连线斜率的最大值即为所求。用sort函数先进行排序

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn=1e5;
struct build{
	int area;
	int price;
};
bool cmp(build&a,build&b)
{
	return a.area<b.area;      
}
int main(void){
	int t,n,flag=0,result,i;
	scanf("%d",&t);
	build house[maxn];
	while(t--){
		double max=0;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d%d",&house[i].area,&house[i].price);
		}
		sort(house,house+n,cmp);
		for(i=0;i<n-1;i++){
			if(house[i].area==house[i+1].area){
				flag=1;
				break;
			}
			int x=abs(house[i].price-house[i+1].price);
			int y=abs(house[i].area-house[i+1].area);
			double result=x*1.0/y;      //把x先化为浮点数,再进行运算,要不会只有整数部分
			if(result>max){
				max=result;
			}
		}
		if(flag==1)
			printf("-1\n");
		else
			printf("%.6f\n",max);
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/han_hhh/article/details/80377926