2000. Toy Shopping 继续是struct
来源:互联网 发布:中国软件学院排名 编辑:程序博客网 时间:2024/05/23 20:19
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
60 521442 210119 100120 108619 74448 10
Sample Output
320623
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;}
- 2000. Toy Shopping 继续是struct
- 2000. Toy Shopping
- Sicily 2000. Toy Shopping
- 2000. Toy Shopping
- Sicily2000——Toy Shopping
- shopping
- shopping
- Shopping
- Shopping
- shopping
- Shopping
- 是typedef struct LNode而不是struct LNode?
- 又是星期一。继续Ranorex
- python \的作用是继续
- 继续模块化,这次是ES6
- 是阿里巴巴继续一家独大还是群雄逐鹿?(1)
- 困惑,是继续2.5还是着手3.0?
- 秋天,是时候继续播种了
- Best Graphics Packs Available!
- 失业的程序员
- 2014云计算三大趋势:Iaas血流成河,Saas持续增长,领域更为细分
- 常用RGB颜色表
- Jquery Ajax WebService
- 2000. Toy Shopping 继续是struct
- 超级强大的心理定律~~!你可以变成你想要的样子!~
- 解决在Hyper-V或SCVMM2012上运行虚拟机或执行实时迁移时,出现error 0x80070569的问题
- 小众品牌如何在互联网风生水起
- C语言编程时常犯的错误
- Linux Make(Makefile)由浅入深的学习与示例剖析
- Bitmap转换为字节码/输出流
- 软件质量的一些小知识
- 2014年最值具有价值的十大行业