Problem Y — limit 1 second
Delayed Work
You own a company that hires painters to paint houses. You can hire as many painters as you want,
but for every painter you hire, you have to pay X dollars (independent of how long the painter
works on the house). In addition, you have to pay a penalty of D · P dollars overall if it takes D
days to finish painting the house. This penalty does not depend on the number of painters you
hire; furthermore, even if the time taken is not a whole number of days, you are only penalized for
the exact time taken.
All painters paint at the same rate. One painter can finish painting the house in K days. The
painters cooperate so well that it will only take K/M days for M painters to finish painting the
house.
What is the minimum amount of money you will have to pay, including what you pay to the painters
and the cost penalty, to get a house painted?
Input
The input consists of a single line containing three space-separated integers K, P, and X (1 ≤
K, P, X ≤ 10,000).
Output
Print, on a single line, the minimum cost to have the house painted, rounded and displayed to
exactly three decimal places.
Sample Input and Output
31 41 59
549.200
3 4 5
6.000
2017 Pacific Northwest Region Programming Contest
题意:一个人画完画要用K天,每一天要交罚金P,如果要用M个人,用K/M天就可以完成,每个人要给X元,求最少花费。
PS* 我用了高中学的(a+b)<=sqrt(2a*b),当且仅当a=b时,等号成立。
#include<bits/stdc++.h>
using namespace std;
int k, p, x, n;
double a, b, num;
int main()
{
scanf("%d %d %d", &k, &p, &x);
n = sqrt(k * p * 1.0 / x * 1.0);
a = k * p * 1.0 / n * 1.0 + x * n * 1.0;
b = k * p * 1.0 / (n + 1) * 1.0 + x * (n + 1) * 1.0;
if(a>b)
{
printf("%.3lf\n", b);
}
else
{
printf("%.3lf\n", a);
}
return 0;
}