您的位置:首页 > 其它

杭电OJ hdu1009 FatMouse' Trade

2016-07-19 13:22 267 查看
仅作为水题学习记录,转载随意,欢迎大神们拍砖

oj地址http://acm.hdu.edu.cn/showproblem.php?pid=1009

Problem Description

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.

The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of
cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All
integers are not greater than 1000.

 

Output

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

 

Sample Input

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

 

Sample Output

13.333
31.500

思路:

先算出每个房间中JavaBean和Cat Food的比重,优先选取比重大的。如第一个测试数据,7/2=3.5,4/3=1.3333,5/2=2.5,先取7和5,最后再按比例在4中取,因此结果为7+5+4*(1/3)=13.333

C++ 代码

/*
注意精度问题,weight要用double,用float会WA。
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

struct Unit
{
<span style="white-space:pre">	</span>int javaBean;
<span style="white-space:pre">	</span>int catFood;
<span style="white-space:pre">	</span>double weight;//精度问题,float产生WA
};

bool compareByWeight(Unit unit1, Unit unit2)
{
<span style="white-space:pre">	</span>return (unit1.weight > unit2.weight);
}

int main()
{
#ifdef ONLINE_JUDGE
#else
<span style="white-space:pre">	</span>streambuf* backup;
<span style="white-space:pre">	</span>ifstream fin;
<span style="white-space:pre">	</span>fin.open("../../../data/hdu1009.txt", ios_base::in);
<span style="white-space:pre">	</span>backup = cin.rdbuf();
<span style="white-space:pre">	</span>cin.rdbuf(fin.rdbuf());
#endif

<span style="white-space:pre">	</span>int M, N;
<span style="white-space:pre">	</span>double result;//JavaBean
<span style="white-space:pre">	</span>int catFood;//cat Food
<span style="white-space:pre">	</span>vector<Unit> data;
<span style="white-space:pre">	</span>while (cin >> M >> N)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>data.clear();
<span style="white-space:pre">		</span>catFood = M;
<span style="white-space:pre">		</span>if (M == -1 && N == -1)
<span style="white-space:pre">			</span>break;
<span style="white-space:pre">		</span>for (int i = 0; i < N; ++i)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>Unit unit;
<span style="white-space:pre">			</span>cin >> unit.javaBean >> unit.catFood;
<span style="white-space:pre">			</span>unit.weight = unit.javaBean / (double)unit.catFood;
<span style="white-space:pre">			</span>data.push_back(unit);
<span style="white-space:pre">		</span>}

<span style="white-space:pre">		</span>sort(data.begin(), data.end(), compareByWeight);

<span style="white-space:pre">		</span>result = 0.0;
<span style="white-space:pre">		</span>for (vector<Unit>::iterator itr = data.begin(); itr != data.end(); ++itr)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>int currCatFood = (*itr).catFood;
<span style="white-space:pre">			</span>int bean = (*itr).javaBean;
<span style="white-space:pre">			</span>if (catFood >= currCatFood)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>catFood -= currCatFood;
<span style="white-space:pre">				</span>result += bean;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>else if (catFood >= 0 && catFood < currCatFood)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>result = result + (catFood / (double)currCatFood) * bean;
<span style="white-space:pre">				</span>break;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>else
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>break;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}

<span style="white-space:pre">		</span>cout.setf(ios::showpoint);
<span style="white-space:pre">		</span>cout.precision(6);
<span style="white-space:pre">		</span>cout.setf(ios::fixed);
<span style="white-space:pre">		</span>cout << result << endl;
<span style="white-space:pre">	</span>}

#ifdef ONLINE_JUDGE
#else
<span style="white-space:pre">	</span>fin.close();
<span style="white-space:pre">	</span>cin.rdbuf(backup);
<span style="white-space:pre">	</span>system("pause");
#endif
<span style="white-space:pre">	</span>return 0;
}


ps:吐槽下,题目简单,但是作为一个OJ新水货,开始weight用的float类型,一直Wrong Answer,搞得我怀疑方法不对,换成double才AC掉,瞬间一万匹草泥马在心中崩腾而过。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM Aglorithm OJ 算法 杭电