zoukankan      html  css  js  c++  java
  • sicily 2000. Toy Shopping

    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

    再做题难度大点的体会下C++的新特性,包括结构体使用时可以省略 struct,在for的head里声明循环变量使程序结构更清晰,还有sort函数的使用
    思路很简单,输入,排序,再输出前三,用时0.06s,不是很好

    View Code
     1 #include<iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 struct toy
     6 {
     7     double hfm;
     8     int index, joy, price;
     9 } a[26000];
    10 
    11 bool cmp( toy a, toy b )
    12 {
    13     return a.hfm > b.hfm;
    14 }
    15 int main()
    16 {
    17     int n;
    18     cin >> n;
    19     
    20     for( int i = 0; i < n; i++ )
    21     {
    22         cin >> a[i].joy >> a[i].price;
    23         a[i].index = i+1;
    24         a[i].hfm = a[i].joy * 1.0 / a[i].price;
    25     }
    26     
    27     sort( a, a + n, cmp );
    28     
    29     int sum = 0;
    30     for ( int i = 0; i < 3; i++ )
    31         sum += a[i].price;
    32     cout << sum << endl;
    33     
    34     for ( int i = 0; i < 3; i++ )
    35         cout << a[i].index << endl;
    36 }
  • 相关阅读:
    如何获取汉字对应的拼音
    php each()函数和list()函数
    php list()函数
    addslashes给预定义字符前面加上反斜杠
    array_filter() 过滤数组中的空白元素
    用.htaccess文件实现URL重写
    xml中实体引用
    onsubmit阻止表单提交
    php获取当前文件绝对路径
    array_merge() 函数的用法
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2893600.html
Copyright © 2011-2022 走看看