Question link
Question meaning:
One row a[i], one row b[i], a and b are in one-to-one correspondence. Select any number pair such that sigma(a)/sigma(b) is equal to k, and find the maximum value of sigma(a) at this time Analysis:
The key to this question is to compare sigma(a)/ Processing of sigma(b)==k. For this formula, it is obviously not possible to use the ratio of each number, because it cannot be accumulated; and it is a double type, so it is impossible to DP
consider the impact of each number pair on this formula. If each number are all a = k * b, then it is obviously possible; if a is less than k * b, then in the whole, the current number with fewer pairs must have some number pairs to compensate, that is, remember x = k * b - a, the sum of x for all selected pairs should be zero.
Then, each number pair actually becomes a number that can be positive or negative. Find the maximum value of sigma (b) of several numbers whose sum is zero, separate the positive and negative, and use a backpack to solve it const int MAXN = 110;const int M = 210000;int tastes[MAXN], calories[MAXN];int v[MAXN];int dp[2][M];int main(){// freopen("in.txt", "r", stdin); int n, m; while (~RII(n, m)) { REP(i, n) RI(tastes[i]); REP(i, n) RI(calories[i]); REP(i, n) v[i] = tastes[i] - calories[i] * m; CLR(dp, -INF); dp[0][0] = dp[1][0] = 0; REP(i, n) { int cnt = v[i] < 0, val = abs(v[i]); FED(j, M - val - 1, 0) dp[cnt][j + val] = max(dp[cnt][j + val], dp[cnt][j] + tastes[i]); } int ans = 0; REP(i, M) ans = max(ans, dp[0][i] + dp[1][i]); if (ans <= 0) puts("-1"); else WI(ans); } return 0;}
Copy after login