Topcoder TriviaGame 动态规划

题目“:

Problem Statement

 

You and your friends have gotten together for a Trivia night at the local pub. Each question is worth a number of points; the i-th element of pointscorresponds to the score received if you correctly answer the i-th question, but you lose that many points if you answer it incorrectly. The questions are given in the order specified in points, and you must answer each question before the next is asked.

In addition, after each correct answer you will receive a token, and you keep all of your tokens if you answer a question incorrectly. If you then havetokensNeeded tokens, the pub will immediately take all of your tokens and award you additional bonus points. The bonuses are different for each question; element i of bonuses corresponds to the bonus you receive if you win the bonus on question i. Note that it is possible to receive multiple bonuses during the game (see example 0).

You know the answer to all the questions and want to maximize the number of points that you receive. Return the maximum points that you can receive if you correctly choose which questions to answer.

Definition

 
Class:TriviaGame
Method:maximumScore
Parameters:vector <int>, int, vector <int>
Returns:int
Method signature:int maximumScore(vector <int> points, int tokensNeeded, vector <int> bonuses)
(be sure your method is public)
 
 

Constraints

-points will contain between 1 and 50 elements, inclusive.
-Each element of points will be between 0 and 1000, inclusive.
-tokensNeeded will be between 1 and 50, inclusive.
-bonus will contain the same number of elements as points.
-Each element of bonus will be between 0 and 10000, inclusive.

Examples

0) 
 
{1, 2, 3, 4, 5, 6}
3
{4, 4, 4, 4, 4, 4}
Returns: 29
Answering all six questions correctly will yield the best score in this case, obtaining the bonus after answering the 3rd and 6th questions.
1) 
 
{1, 2, 3, 4, 5, 6}
3
{1, 1, 1, 20, 1, 1}
Returns: 39
Here, it is better to get the first question wrong so that you get the big bonus on the 4th question.
2) 
 
{150, 20, 30, 40, 50}
3
{0, 0, 0, 250, 0}
Returns: 500
Here, your optimal strategy is to get the second question wrong. Then, you will have 3 tokens after the 4th question, so that you can cash in on the big bonus.
3) 
 
{500, 500, 500, 0, 500}
3
{0, 0, 0, 500, 0}
Returns: 2000

典型的动态规划:

#include <string>
#include <vector>
#include <algorithm>

using namespace std;
class TriviaGame {
	public:
	int maximumScore(vector <int> points, int tokensNeeded, vector <int> bonuses);
};

int TriviaGame::maximumScore(vector <int> points, int tokensNeeded, vector <int> bonuses){
	int r,memo[55][55];
	int sz=points.size();
	for(int i=0;i<tokensNeeded;i++)
		memo[sz][i] = 0;
	for(int i=sz-1;i>=0;i--)
	{
		for(int j=0;j<tokensNeeded;j++)
		{
			if(j <= tokensNeeded - 2)
				memo[i][j] = max(memo[i+1][j+1] + points[i],memo[i+1][j] - points[i]);
			else
				memo[i][j] = max(memo[i+1][0] + points[i] + bonuses[i],memo[i+1][j] - points[i]);
		}
	}
	return memo[0][0];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值