250:
有n首歌每首歌有duration和tone,连续唱m首歌会消耗每首歌的duration以及相邻两首歌的tone的差的绝对值的和,给个T,问说在T时间内最对能唱多少歌。
将歌按tone排序后发现,只要枚举连续歌的最左和最右,这样产生的收尾的tone的绝对值的差的和是不变的,然后将中间的歌按duration从小到大唱,知道时间T消耗完毕。
500:
要发现访问若干room后获取到的钥匙的数量与访问的顺序是无关的,用dp(i, j)表示访问过i(i表示访问过的room的二进制状态压缩)后,手里拥有j把红钥匙的白钥匙最多的数量。
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
using namespace std; typedef vector<int> VI; int dp[ << ][];
int num[ << ]; class KeyDungeonDiv1 {
private:
int n;
//map<int, int> dp_;
public:
KeyDungeonDiv1() {
memset(dp, -, sizeof(dp));
}
int get_dp(int i, int k) {
return dp[i][k];
/*
int idx = i * 131 + k;
if (dp_.find(idx) == dp_.end()) return -1;
else return dp_[idx];
*/
}
void set_dp(int i, int k, int v) {
dp[i][k] = v;
/*
int idx = i * 131 + k;
dp_[idx] = v;
*/
}
bool open_door(int& r, int& g, int& w, int dr, int dg) {
int need = ;
if (dr - r > ) need += (dr - r);
if (dg - g > ) need += (dg - g);
if (need <= w) {
r -= dr;
g -= dg;
if (r < ) {
w += r;
r = ;
}
if (g < ) {
w += g;
g = ;
}
return true;
} else {
return false;
}
}
int maxKeys(VI door_r, VI door_g, VI room_r, VI room_g, VI room_w, VI keys) {
int ans = keys[] + keys[] + keys[];
n = door_r.size();
memset(num, -, sizeof(num));
set_dp( << n, keys[], keys[]);
num[ << n] = keys[] + keys[] + keys[];
for (int i = ; i < ( << (n + )); i++) {
for (int k = ; k <= ; k++) {
if (get_dp(i, k) != -) {
for (int u = ; u < (n + ); u++) if ((i & ( << u)) == ) {
int w = get_dp(i, k);
int r = k;
int g = num[i] - w - r;
if (open_door(r, g, w, door_r[u], door_g[u])) {
if (num[i | ( << u)] == -) num[i | ( << u)] = r + g + w + room_r[u] + room_g[u] + room_w[u];
if (w + room_w[u] > get_dp(i | ( << u), r + room_r[u])) {
set_dp(i | ( << u), r + room_r[u], w + room_w[u]);
if (num[i | ( << u)] > ans) {
ans = num[i | ( << u)];
}
}
}
}
}
}
}
return ans;
}
};