3126. 商品推荐

单点时限: 2.0 sec

内存限制: 256 MB

一个电子商务网站要向顾客推荐商品,可以让顾客指定规则。

现在某类顾客指定的规则是:对于同一类商品,如果一个商品的销量高于所有该类商品销量的中位数,并且价格低于所有该类商品价格的中位数 *,则向他推荐,显示商品编号,销量和价格。

如果有多个商品符合要求,推荐的顺序是按销量从大到小排序,销量一样则按价格从低到高排序。如果销量价格都一样,按输入顺序排序。如果没有符合要求的商品,则提示不推荐,显示 no recommendation。

中位数定义:
一组数据 (a1,a2,a3,…,an) 按从小到大(或从大到小)的顺序排列,如果数据个数 n 为奇数(即,n=2∗k+1),则排在中间位置(第 k+1 个)的数 ak+1 为中位数;如果数据个数为偶数(n=2∗k),则取最中间两个数据的平均值为中位数 (即,第 k 位和第 k+1 位两个数的平均值为中位数,(ak+ak+1)/2。

输入格式
第 1 行:一个整数 T (1⩽T⩽10) 为问题数。

对于每一组测试数据按如下格式输入:

第 1 行:1 个正整数 n(1⩽n⩽100),表示商品的个数

第 2 行 ∽n+1 行:商品编号(不超过 9 个字符),销量(非负整数,在 32 位整型表示以内)和价格(非负数,在 32 位浮点型表示以内)。

数据之间用一个空格分隔。

输出格式
对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。

然后对应每个问题,按符合条件的商品数目,每行输出一组商品编号,销量和价格,格式与输入时一样。无符合的则显示 no recommendation。

样例
input
3
2
11 2 7
12 1 6
5
e01 0 7
e02 87 5.5
e03 8 10
e04 97 6
e05 55 8
8
a100 201 23.0
a101 518 19.7
a102 289 36.7
a103 218 22.9
a104 37 1.2
a105 515 42.0
a106 577 25.6
a107 136 44.5
output
case #0:
no recommendation
case #1:
e04 97 6
e02 87 5.5
case #2:
a101 518 19.7

/*
思路:两次排序。比较中位数时需要注意,因为排序后是数有可能中位数前面的一些数和中位数相同。
*/
#include<iostream>
#include<algorithm>
using namespace std;
struct g {
	string name;
	double sum;
	double price;
	int index;
	int flag1=0;
	int flag2=0;
};
bool cmp1(g a,g b) {
	return a.price<b.price;
}
bool cmp2(g a,g b) {
	if(a.sum!=b.sum)
		return a.sum>b.sum;
	else {
		if(a.price!=b.price)
			return a.price<b.price;
		return a.index<b.index;
	}
}
int main() {
	int t;
	cin>>t;
	for(int i = 0; i < t; i++) {
		int n;
		cin>>n;
		g G[n];
		for(int i = 0; i < n; i++) {
			cin>>G[i].name>>G[i].sum>>G[i].price;
			G[i].index=i;
		}
		sort(G,G+n,cmp1);
		for(int i = 0; i < (n+1)/2; i++) {
			if(n%2==0&&2*G[i].price<G[n/2-1].price+G[n/2].price) {
				G[i].flag1=1;
			}
			if(n%2==1&&G[i].price<G[n/2].price) {
				G[i].flag1=1;
			}
		}
		sort(G,G+n,cmp2);
		for(int i =0 ; i < (n+1)/2; i++) {
			if(n%2==1&&G[i].sum>G[n/2].sum) {
				G[i].flag2=1;
			}
			if(n%2==0&&G[i].sum*2>G[n/2-1].sum+G[n/2].sum) {
				G[i].flag2=1;
			}
		}
		int flag=0;
		printf("case #%d:\n",i);
		for(int i = 0; i < (n+1)/2; i++) {
			if(G[i].flag1&&G[i].flag2) {
				flag=1;
				cout<<G[i].name<<" "<<G[i].sum<<" "<<G[i].price<<endl;
				
			}
		}
		if(flag==0)
			cout<<"no recommendation"<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40394960/article/details/105861178
今日推荐