Topcoder SRM565 DIV2 500分-c++

Manao在途中遇到战斗力和贿赂需求不同的怪物。他必须贿赂怪物以增加战斗力,防止被攻击。问题转化为寻找经过所有怪物的最低贿赂总费用。这可以通过动态规划来解决,状态定义为(i, j),表示在第i个怪物处拥有j战斗力。最优解是找到money(n, j)的最小值。" 79620311,7242986,RLlib:可组合和可扩展的强化学习库,"['强化学习', '深度学习', '机器学习库', '分布式计算', '超参数调整']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

 Manao is traversing a valley inhabited by monsters. During his journey, he will encounter several monsters one by one. The scariness of each monster is a positive integer. Some monsters may be scarier than others. The i-th (0-based index) monster Manao will meet has scariness equal to dread[i].

Manao is not going to fight the monsters. Instead, he will bribe some of them and make them join him. To bribe the i-th monster, Manao needs price[i] gold coins. The monsters are not too greedy, therefore each value in price will be either 1 or 2.

At the beginning, Manao travels alone. Each time he meets a monster, he first has the option to bribe it, and then the monster may decide to attack him. A monster will attack Manao if and only if he did not bribe it and its scariness is strictly greater than the total scariness of all monsters in Manao's party. In other words, whenever Manao encounters a monster that would attack him, he has to bribe it. If he encounters a monster that would not attack him, he may either bribe it, or simply walk past the monster.



Consider this example: Manao is traversing the valley inhabited by the Dragon, the Hydra and the Killer Rabbit. When he encounters the Dragon, he has no choice but to bribe him, spending 1 gold coin (in each test case, Manao has to bribe the first monster he meets, because when he travels alone, the total scariness of monsters in his party is zero). When they come by the Hydra, Manao can either pass or bribe her. In the end, he needs to get past the Killer Rabbit. If Manao bribed the Hydra, the total scariness of his party exceeds the Rabbit's, so they will pass. Otherwise, the Rabbit has to be bribed for two gold coins. Therefore, the optimal choice is to bribe the Hydra and then to walk past the Killer Rabbit. The total cost of getting through the valley this way is 2 gold coins.

You are given the vector <int>s dread and price. Compute the minimum price Manao will pay to safely pass the valley.

Definition

 
Class:MonstersValley2
Method:minimumPrice
Parameters:vector <int>, vector <int>
Returns:int
Method signature:int minimumPrice(vector <int> dread, vector <int> price)
(be sure your method is public)

Limits

 
Time limit (s):2.000
Memory limit (MB):64

Constraints

-dread will contain between 1 and 20 elements, inclusive.
-Each element of dread will be between 1 and 2,000,000,000, inclusive.
-price will contain between the same number of elements as dread.
-Each element of price will be either 1 or 2.

Examples

0) 
 
{8, 5, 10}
{1, 1, 2}
Returns: 2
The example from the problem statement.
1) 
 
{1, 2, 4, 1000000000}
{1, 1, 1, 2}
Returns: 5
Manao has to bribe all monsters in the valley.
2) 
 
{200, 107, 105, 206, 307, 400}
{1, 2, 1, 1, 1, 2}
Returns: 2
Manao can bribe monsters 0 and 3.
3) 
 
{5216, 12512, 613, 1256, 66, 17202, 30000, 23512, 2125, 33333}
{2, 2, 1, 1, 1, 1, 2, 1, 2, 1}
Returns: 5
Bribing monsters 0, 1 and 5 is sufficient to pass safely.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

题目大意:

Manao要依次经过一些怪物,每个怪物具有两个属性战斗力和贿赂所需的金钱,一开始Manao没有战斗力,他通过贿赂怪兽使其成为自己的战斗力,要是Manao当前经过的怪兽的战斗力比Manao的战斗力高,怪兽就会攻击他,Manao就必须贿赂怪兽,反之,Manao可以选择贿赂或直接过去,求经过一系列怪兽所花费的最少的金钱数。

此题经分析为动态规划,要求的最优解为最少钱数,而状态为(i,j),表示当前经过第i个怪兽时战斗力为j,money(i,j)->money(i+1,j+dread[i])+price[i]或money(i+1,j),答案是money(n,j)中最小值。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#define INF 0xfffffff
using namespace std;

class MonstersValley2
{
public:
	typedef map<int, int> MAP;
	int minimumPrice(vector<int> dread, vector<int> price)
	{	
		size_t n = dread.size();
		vector<MAP> m(n);
		m[0][dread[0]] = price[0];
		int i = 1, k = INF;
		while (i<n)
		{
			k = INF;
			for (MAP::iterator it = m[i-1].begin(); it != m[i-1].end(); ++it)
			{
				if (it->first < dread[i])
				{
					int t;
					t = m[i][it->first + dread[i]] = it->second + price[i];
					if (k > t)
						k = t;
				}

				else
				{
					int t;
					t = m[i][it->first] = it->second;
					m[i][it->first + dread[i]] = it->second + price[i];
					if (k > t)
						k = t;
				}
			}
			++i;
		}
		return k;
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值