Sicily 1099 Packing Passengers
2016-03-16 13:39
330 查看
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
PTA, Pack ‘em Tight Airlines is attempting the seemingly impossible—to fly with only full planes and still make a profit. Their strategy is simplicity and efficiency. Their fleet consists of 2 types of equipment (airline lingo for airplanes). Type A aircraftcost costA dollars to operate per flight and can carry passengersA passengers. Type B aircraft cost costB dollars to operate per flight and can carry passengersB passengers.
PTA has been using software that works well for fewer than 100 passengers, but will be far too slow for the number of passengers they expect to have with larger aircraft. PTA wants you to write a program that fills each aircraft to capacity (in keeping with
the name Pack 'em Tight) and also minimizes the total cost of operations for that route.
Input
The input file may contain data sets. Each data set begins with a line containing the integer n (1 <= n <= 2,000,000,000) which represents the number of passengers for that route. The second line contains costA and passengersA, and the third line containscostB and passengersB. There will be white space between the pairs of values on each line. Here, costA, passengersA, costB, and passengersB are all nonnegative integers having values less than 2,000,000,001.
After the end of the final data set, there is a line containing “0” (one zero) which should not be processed.
Output
For each data set in the input file, the output file should contain a single line formatted as follows:Data set <N>: <A> aircraft A, <B> aircraft B
Where <N> is an integer number equal to 1 for the first data set, and incremented by one for each subsequent data set, <A> is the number of airplanes of type A in the optimal solution for the test case, and <B> is the number of airplanes of type B in the optimal
solution. The 'optimal' solution is a solution that lets PTA carry the number of passengers specified in the input for that data set using only airplanes loaded to their full capacity and that minimizes the cost of operating the required flights. If multiple
alternatives exist fitting this description, select the one that uses most airplanes of type A. If no solution exists for PTA to fly the given number of passengers, the out line should be formatted as follows:
Data set <N>: cannot be flown
Sample Input
600 30 20 20 40 550 1 13 2 29 549 1 13 2 29 2000000000 1 2 3 7 599 11 20 22 40 0
Sample Output
Data set 1: 0 aircraft A, 15 aircraft B Data set 2: 20 aircraft A, 10 aircraft B Data set 3: 11 aircraft A, 14 aircraft B Data set 4: 6 aircraft A, 285714284 aircraft B Data set 5: cannot be flown
Solution
给出总的人数total,给出costA和passA,costB和passB,求出一组解(x,y)使得在满足x*passA + y*passB的时候min{x*costA + y*costB}。这是一个简单的数学题,注意边界条件就可以了。
用y表示x,然后代入min{x*costA + y*costB}中得到一个一元方程,分类讨论y的最优解即可。注意极端数据0 0 0 0即可,中间计算结果用long long。
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll tot, count = 0;
while (scanf("%lld", &tot) != EOF && tot)
{
ll a, b, c, d, x, y;
bool has = true;
bool flag = false;
scanf("%lld%lld%lld%lld", &a, &c, &b, &d);
if (c == 0 || d == 0)
{
if (d == 0 && c == 0) has = false;
else if (d == 0 && c != 0)
{
if (tot % c) has = false;
else x = tot / c;
}
else if (c == 0 && d != 0)
{
if (tot % d) has = false;
else y = tot / d;
}
}
else
{
if (a*d <= b*c)
{
swap(a, b);
swap(c, d);
flag = true;
}
if (a*d == b*c) y = tot / d;
else y = min(tot / d, a*tot/(a*d-c*b));
x = (tot - y*d) / c;
while (x*c + y*d != tot && y > 0)
{
--y;
x = (tot - y*d) / c;
}
if (y == 0 && x*c + y*d != tot) has = false;
}
count ++;
printf("Data set %lld: ", count);
if (!has) printf("cannot be flown\n");
else
{
if (flag) printf("%lld aircraft A, %lld aircraft B\n", y, x);
else printf("%lld aircraft A, %lld aircraft B\n", x, y);
}
}
return 0;
}
相关文章推荐
- 一道2年级数学题 10人做9人错
- HDU-1163Eddy's digital Roots
- sicily 1020. Big Integer
- soj1041. Pushing Boxes
- Sicily 1046, Plane Spotting
- SICILY 1934 移动小球
- TOJ 3051
- TOJ 1537
- TOJ 1252
- TOJ 2544
- TOJ 2399
- TOJ 2646
- TOJ 1981
- TOJ 2814
- TOJ 1255
- 1920. Divide The Stones
- 1801. Reading books
- 1874. Three little pigs
- 导游 解题报告(Floyd-Warshall 算法)
- Sicily 1121. Tri Tiling