2000. Toy Shopping 继续是struct

2000. Toy Shopping

Constraints

Time Limit: 1 secs, Memory Limit: 64 MB

Description

Bessie wants some toys. She's been saving her allowance for years, and has an incredibly huge stash. However, she is quite frugal and wants to get the best value for her cash. In fact, she has decided only to buy exactly three different toys of the N (3 <= N <= 25,000) offered at the Bovine Plaything Palace. 
Toy i brings Bessie J_i (0 <= J_i <= 1,000,000) microbundles of joy and and has price P_i (0 < P_i <= 100,000,000). Bessie has enough money to buy any three toys that she chooses. 
Bessie wants to maximize the sum of her happy-frugal metric (which is calculated as J_i/P_i -- joy divided by price) for the three toys she chooses. Help Bessie decide which toys she should buy. The answer is guaranteed to be unique. 
Assume that the Bovine Plaything Palace offers 6 different toys for Bessie: 

        i    Joy       Price       Happy-Frugal Metric

        -    ---       -----       -------------------

        1      0        521               0.00000

        2    442        210               2.10476...

        3    119        100               1.19000

        4    120        108               1.11111...

        5    619        744               0.83198...

        6     48         10               4.80000

Bessie would choose toy 6 (HFM = 4.80), toy 2 (HFM = 2.10), and toy 3 (HFM = 1.19).

Input

* Line 1: A single integer: N 
* Lines 2..N+1: Line i+1 contains two space-separated integers: J_i and P_i

Output

* Line 1: The total price that Bessie will have to pay 
* Lines 2..4: In descending order sorted by the happy-frugal metric, the 1-based index of the toys that Bessie should buy, one per line

Sample Input

6
0 521
442 210
119 100
120 108
619 744
48 10

Sample Output

320
6
2
3

Problem Source

2010中山大学新手赛-网络预选赛


第一次先用了冒泡排序 结果在第10个case的时候TLE了 

代码如下:

#include <iostream>

using namespace std;

struct Toy {
	int num;
	int joy;
	int price;
	double HFM;
}toy[25001];
int main () {
	int n;
	cin>>n;
	for (int i = 1; i <= n; i++) { //输入
		toy[i].num = i;
		cin>>toy[i].joy;
		cin>>toy[i].price;
		toy[i].HFM = (double)toy[i].joy / (double)toy[i].price;
	}
	Toy temp;
	for (int i = 1; i <= n; i++) {  //冒泡
		for (int j = 1; j <= n - i; j++) {
			if (toy[j].HFM < toy[j+1].HFM) {
				temp = toy[j];
				toy[j] = toy[j+1];
				toy[j+1] = temp;
			}
		}
	}

	int count = 0;
	for (int i = 1; i <= 3; i++) {  //计算价格
		count += toy[i].price;
	}
	cout<<count<<endl;
	for (int i = 1; i <= 3; i++) {
		cout<<toy[i].num<<endl;
	}
	
	//system("pause");
	return 0;
}

后来去掉了冒泡,直接用一个数组记录HFM最大的3个toy的下标,然后直接输出就AC了。

代码如下:

#include <iostream>

using namespace std;

struct Toy {
	int num;
	int joy;
	int price;
	double HFM;
}toy[25001];

int main () {
	int n;
	cin>>n;
	for (int i = 1; i <= n; i++) {
		toy[i].num = i;
		cin>>toy[i].joy;
		cin>>toy[i].price;
		toy[i].HFM = (double)toy[i].joy / (double)toy[i].price;
	}
	int count = 0;

	int a[4] = {0};
	int k = 0;
	while (k < 3) {
		double max = toy[0].HFM;
		for (int i = 1; i <= n; i++) {
			if (max < toy[i].HFM) {
				max = toy[i].HFM;
				a[k] = i;
			}
		}
		toy[a[k]].HFM = 0;
		k++;
	}
	
	for (int i = 1; i <= 3; i++) {
		count += toy[a[i - 1]].price;
	}
	cout<<count<<endl;

	for (int i = 1; i <= 3; i++) {
		cout<<toy[a[i - 1]].num<<endl;
	}
	
	//system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值