思路:
dp,递推关系优化。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 int n, m, h, p, tmp, maxH; 7 int a[2005][2005], dp[2005][2005]; 8 9 void solve() 10 { 11 for (int j = maxH; j >= 0; j--) 12 { 13 int tmp = 0; 14 for (int k = 0; k < n; k++) 15 { 16 if (j + h <= maxH) 17 tmp = max(tmp, dp[k][j + h]); 18 } 19 for (int i = 0; i < n; i++) 20 { 21 dp[i][j] = tmp; 22 if (j + 1 <= maxH) 23 dp[i][j] = max(dp[i][j], dp[i][j + 1]); 24 dp[i][j] += a[i][j]; 25 } 26 } 27 int maxn = 0; 28 for (int i = 0; i < n; i++) 29 { 30 maxn = max(maxn, dp[i][0]); 31 } 32 cout << maxn << endl; 33 } 34 35 int main() 36 { 37 cin >> n >> m >> h; 38 for (int i = 0; i < n; i++) 39 { 40 scanf("%d", &p); 41 for (int j = 0; j < p; j++) 42 { 43 scanf("%d", &tmp); 44 maxH = max(maxH, tmp); 45 a[i][tmp]++; 46 } 47 } 48 solve(); 49 return 0; 50 }