WA 的分析
1. solve_dp 返回的是 double 不是 int 浪费了非常久的时间
2. 做动规, 当状态转移方程比较复杂时, 举例子能够帮助初始化和写出状态转移方程
总结
1. 这里 dp 的设置是 tight, 一般 tight 都是比较难的题目
#include <iostream> #include <stdio.h> #include <memory.h> #include <algorithm> #include <vector> #include <map> #include <set> #include <string> #include <deque> #include <cstring> #define MIN(x,y) (x)<(y)?(x):(y) #define MAX(x,y) (x)>(y)?(x):(y) using namespace std; vector<int> money[200]; vector<int> bandWidth[200]; int dp[200][200]; // tight, not exactly // handle to decide the iterator function // use examples to help understand double solve_dp(int n, int maxbw) { memset(dp, 0x3f, sizeof(dp)); // init dp[0][0] = 0; for(int i = 0; i < money[0].size(); i ++) { for(int j = 1; j <= bandWidth[0][i]; j ++) { dp[0][j] = min(dp[0][j], money[0][i]); } } // main procedure for(int i = 1; i < n; i ++) { for(int j = 0; j <= maxbw; j ++) { bool init = false; for(int k = 0; k < money[i].size(); k ++) { if(j > bandWidth[i][k]) continue; if(init) { dp[i][j] = dp[i-1][j] + money[i][k]; init = true; } else { dp[i][j] = min(dp[i][j], dp[i-1][j] + money[i][k]); } } } } double res = 0.0; for(int i = 1; i <= maxbw; i ++) { if(dp[n-1][i] == 0x3f3f3f3f) continue; res = MAX(res, i*1.0/dp[n-1][i]); } return res; } int main() { freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin); int T, N; scanf("%d", &T); while(T--) { scanf("%d", &N); int maxbw = 0; for(int i = 0; i < N; i ++) { int number; scanf("%d", &number); money[i].clear(); bandWidth[i].clear(); for(int j = 0; j < number; j ++) { int bw, pc; scanf("%d%d", &bw, &pc); bandWidth[i].push_back(bw); money[i].push_back(pc); maxbw = max(maxbw, bw); } } double res = solve_dp(N, maxbw); printf("%0.3f ", res); } return 0; }