Thought suddenly thought, transfer equation but did not think well, see on-line a way of thinking the same code, change the transfer equation.
At the same time DP all initialized to negative infinity, need to pay attention to.
The AC code is as follows:
/**************************************************memory:1884 kbtime:250 mslanguage:g++result:accepted********* /#include <iostream> #include <cstdio> #include <cstring >using namespace std;/** title Description: N Cow (1-n), each ox has two values Si and fi, select any of them, make S and F's and Max, where S and f each and both cannot be negative */int s[105], F[105];int dp[ 200005];int num[200005];const int INF = 0x5f5f5f5f;int Main () {int n; while (CIN >> N) {int m = 0; for (int i = 0; i < n; ++i) {cin >> s[i] >> f[i]; S[i] + = 1000; All plus 1000 guaranteed non-negative m + = s[i]; } for (int i = 0; I <= m; ++i) dp[i] =-inf; memset (num, 0, sizeof num); Dp[0] = 0; Ensure that all DP values are exactly full. for (int i = 0, i < n; ++i) {for (int j = m; J >= S[i];--j) {//if (Dp[j] < Dp[j-s[i]] + f[i]) if (Dp[j]-num[j] * < Dp[j-s[i]] + f[i]-(Num[j-s[i]] + 1) * 1000) {//This transfer equation I was drunk, too. Guaranteed is s and for J time S and F's and max dp[j] = dp[j-s[i]] + f[i]; NUM[J] = Num[j-s[i]] + 1; Record how many cows are used. }//printf ("dp[%d]=%d, num[%d]=%d\n", J, Dp[j],j, Num[j]); }} int ans = 0; for (int j = 0; j <= m; ++j) {if (Dp[j] >= 0 && j-num[j] * + >= 0) ans = max (ans, dp[j] + j-num[j] * 1000); } cout << ans << endl; } return 0;}
Poj2184cow Exhibition (01 backpack variant)