题目:http://community.topcoder.com/stat?c=problem_statement&pm=12924&rd=15820
由于X太大(1e9),所以使用 map 保存结果,dp[i][x] 表示参加i次比赛后 rating 为 x 时的最多颜色变化次数。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair
/*************** Program Begin **********************/
//map <long long, int> dp[55];
class TypoCoderDiv1 {
public:
int getmax(vector <int> D, int X) {
map <long long, int> dp[55];
int res = 0;
int n = D.size();
dp[0][X] = 0;
long long pre, cur;
for (int i = 1; i <= n; i++) {
for (auto it = dp[i-1].begin(); it != dp[i-1].end(); it++) {
pre = (*it).first; // 之前分数
if ( pre >= 2200 ) {
// must decrease
cur = max(0LL, pre - D[i-1]);
if (cur < 2200) {
dp[i][cur] = max( dp[i][cur], (*it).second + 1 );
}
} else {
// decrease
cur = max(0LL, pre - D[i-1]);
dp[i][cur] = max( dp[i][cur], (*it).second );
// increase
cur = pre + D[i-1];
dp[i][cur] = max(dp[i][cur], (*it).second + ( cur >= 2200 ? 1 : 0 ) );
}
}
}
for (auto it = dp[n].begin(); it != dp[n].end(); it++) {
res = max( res, (*it).second );
}
return res;
}
};
/************** Program End ************************/