2000. Toy Shopping
限制条件
时间限制: 1 秒, 内存限制: 64 兆
题目描述
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).
输入格式
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains two space-separated integers: J_i and P_i
输出格式
* 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
样例输入
6 0 521 442 210 119 100 120 108 619 744 48 10
样例输出
320 6 2 3真是要哭了,原来只是sort()函数少写了一个while!!!
题目看起来很简单,就是根据h的大小找到最大的三个,输出其位置,可以直接找,也可以排序!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <map>
#include <vector>
#include <cmath>
#include <string>
#include <set>
#include <sstream>
#include <cstring>
using namespace std;
struct Toy{
int i;
long double j;
long p;
long double h;
};
void sort(Toy toy[],int l,int r)
{
if(l<r)
{
Toy tem = toy[l];
int i = l,j = r;
while(i<j)
{
while(i < j && toy[j].h < tem.h)
j--;
if(i<j)
toy[i++] = toy[j];
while(i < j && toy[i].h >= tem.h)
i++;
if(i<j)
toy[j--] = toy[i];
}
toy[i] = tem;
sort(toy,l,i-1);
sort(toy,j+1,r);
}
}
int main()
{
int n;
cin >> n;
Toy toy[25001];
for(int i = 1; i <= n; i++)
{
toy[i].i = i;
cin >> toy[i].j >> toy[i].p;
toy[i].h = toy[i].j/toy[i].p;
}
sort(toy,1,n);
long sum = toy[1].p + toy[2].p + toy[3].p;
cout << sum << endl;
for(int i = 1; i <= 3; i++)
{
cout << toy[i].i << endl;
}
return 0;
}