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[1 << 13][131]; int num[1 << 13]; class KeyDungeonDiv1 { private: int n; //map<int, int> dp_; public: KeyDungeonDiv1() { memset(dp, -1, 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 = 0; if (dr - r > 0) need += (dr - r); if (dg - g > 0) need += (dg - g); if (need <= w) { r -= dr; g -= dg; if (r < 0) { w += r; r = 0; } if (g < 0) { w += g; g = 0; } 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[0] + keys[1] + keys[2]; n = door_r.size(); memset(num, -1, sizeof(num)); set_dp(1 << n, keys[0], keys[2]); num[1 << n] = keys[0] + keys[1] + keys[2]; for (int i = 0; i < (1 << (n + 1)); i++) { for (int k = 0; k <= 130; k++) { if (get_dp(i, k) != -1) { for (int u = 0; u < (n + 1); u++) if ((i & (1 << u)) == 0) { 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 | (1 << u)] == -1) num[i | (1 << u)] = r + g + w + room_r[u] + room_g[u] + room_w[u]; if (w + room_w[u] > get_dp(i | (1 << u), r + room_r[u])) { set_dp(i | (1 << u), r + room_r[u], w + room_w[u]); if (num[i | (1 << u)] > ans) { ans = num[i | (1 << u)]; } } } } } } } return ans; } };