现在的位置: 首页 > 综合 > 正文

Topcoder TriviaGame 动态规划

2018年04月04日 ⁄ 综合 ⁄ 共 2486字 ⁄ 字号 评论关闭
文章目录

题目“:

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];
}

抱歉!评论已关闭.