HDU-5931 Mission Possible (线性规划+贪心)

Mission Possible

Time Limit: 36000/18000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 418    Accepted Submission(s): 85

 

Problem Description

Mr. Frog loves playing video games. However, it seems that he does not have enough gift in this field, so he often gets stuck in some certain levels. Now this annoying situation comes to him again.

In this mission, Mr. Frog is asked to pass a
dangerous region which is currently under his enemies, control. Every second he is inside this region, his enemies would attack him with everything they have. Luckily, he is wearing a powersuit and still have time to enhance it.

powersuit has three main attributes: health point, velocity and recover speed. In the begin¬ning of this mission, the powersuit has H health points, and is able to move at no faster than Vm/s. Once the health point of powersuit is less than 0, the powersuit would be totally destroyed and Mr. Frog would immediately be killed. At the end of every second that the powersuit still works, the suit would repair itself and recover R health points.

After precise calculation, Mr. Frog has found that, to go through this area, he needs to run at least D meters distance, and every second his enemies, attack would cause A health point loss. Note that attacks take place all the time while the recover only happens at the end of every second, so you can consider that, in every whole second, the powersuit first loses A health points and then recovers R health points at the end of it, if it still works at that moment.

Mr. Frog could enhance his powersuit at any time he wants during the mission. At the beginning all three attributes of his powersuit equals to 0. He needs to pay G1 in order to increase H by 1, while G2 to increase V by 1 and G3 to increase R by 1. For any of these three values, he can only increase it by non-negative integer. For some well-known reasons, Mr. Frog does not want to finish the game too quickly, so you cannot increase the powersuit's speed to more than D m/s. Since you looks so clever, now Mr. Frog wants to know not only how he can finish this mission, but also the way to spend the least money, can you help him?

Input

The first line contains only one integer T, which indicates the number of test cases. For each test case,there are five integers D,A,G1,G2,G3.

Output

For each test case,output one line “Case #x: Ans’’,where x is the case number (starting from 1) and Ans is the minimum cost required to finish the mission.

Sample Input

2 5 1 1 2 5 10 1 1 2 5

Sample Output

Case #1: 7 Case #2: 8

Hint

1 <= D,A <= 500000, 1 <= G1, G2, G3 <= 200

题意:一个人从起点出发要走过距离D,每秒他会收到A点伤害(持续受到),他的初试血量H=0,速度V=0,生命回复R=0(每秒末回复R点生命值,不是持续的)。提升一点血量、速度、生命回复的花费分别为GH、GV、GR。要求走到终点的过程中生命值不能低于0(可以等于0),问最小花费是多少。

题解:可以想到首先需要枚举速度V,然后可以得到time = D / V,设rt = floor(time),在此time,rt以及提升速度的花费是固定的

可以得到方程:

H=A*time-R*rt

cost=H*GH+R*GR

题目要求最小化cost,整理可以得到:

cost=R*(GR-GH*rt)+GH*A*time

发现除了R都是定值,由于GR-GH*rt可能为正也可能为负,因此只要考虑R取最大值或取最小值的情况即可。

#include
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i=b;--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define add(x,y) x=((x)+(y)>=mod)?(x)+(y)-mod:(x)+(y)
#define sub(x,y) x=((x)-(y)<0)?(x)-(y)+mod:(x)-(y)
#define eps 1e-10
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector VI;
typedef pair PII;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MX = 1e5 + 5;

ll D, A, a, b, c;

ll cal(int v) {
    ll t = D / v;
    return min((ll)ceil(1.0 * A * D / v) * a, A * (a + c));
}

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    int T; cin >> T;
    rep(cas, 1, T + 1) {
        cin >> D >> A >> a >> b >> c;
        printf("Case #%d: ", cas);
        ll ans = INFLL;
        rep(v, 1, D + 1) ans = min(ans, b * v + cal(v));
        cout << ans << endl;
    }
    return 0;
}

 

你可能感兴趣的:(贪心)