Problem Statement | |||||||||||||
Fox Ciel was very bored, so she invented a single player game. The rules of the game are: You start with 0 points. You make exactly nA+nB moves. You have two types of moves available. These are called move A and move B. Exactly nA times you will make move A. Exactly nB times you will make move B. The moves can be in any order. The moves affect your score in the following ways:
You are given int nA, int nB, int paramA and int paramB. Calculate scoreA and scoreB as follows ("/" denotes exact division, without any rounding): scoreA = paramA/1000.0 scoreB = paramB/1000.0 Return the maximum possible score after nA+nB moves. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | The returned value must have an absolute or relative error less than 1e-9. | ||||||||||||
Constraints | |||||||||||||
- | nA will be between 0 and 50, inclusive. | ||||||||||||
- | nB will be between 0 and 50, inclusive. | ||||||||||||
- | paramA will be between -10000 and 10000, inclusive. | ||||||||||||
- | paramB will be between -2000 and 2000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
| |||||||||||||
5) | |||||||||||||
|
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.
【题解】
dp,设f[i][j]为用了i次A,j次B后最大得分,g[i][j]为用了i次A,j次B后最小得分。
则很容易dp,详见代码
【代码】
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double oo=1e10;
double f[60][60],g[60][60];
class FoxPlayingGame
{
public:
double theMax(int, int, int, int);
};
double FoxPlayingGame::theMax(int na, int nb, int parama, int paramb)
{
f[0][0]=g[0][0]=0;
double sa=parama/1000.0;
double sb=paramb/1000.0;
int i,j;
for (i=0;i<=na;i++)
for (j=0;j<=nb;j++)
if (i || j)
{
f[i][j]=-oo;g[i][j]=oo;
if (i)
{
f[i][j]=max(f[i][j],f[i-1][j]+sa);
g[i][j]=min(g[i][j],g[i-1][j]+sa);
}
if (j)
{
if (sb>0) f[i][j]=max(f[i][j],f[i][j-1]*sb);
else f[i][j]=max(f[i][j],g[i][j-1]*sb);
if (sb>0) g[i][j]=min(g[i][j],g[i][j-1]*sb);
else g[i][j]=min(g[i][j],f[i][j-1]*sb);
}
}
return max(f[na][nb],g[na][nb]);
}