Cow Exhibition POJ - 2184

Topic Address: https://vjudge.net/problem/POJ-2184

The following explanation is that from a big brother moved in, said very clearly
meaning of the questions: Given some of the cows, each cow has two property values s and f, positive or negative, asked to select some of the cattle,
so that these and the two attributes of cattle and the largest increase, and these two attributes are seeking plus cattle and can not be negative.
Analysis: dp, began to think dp [i] [s] [ f], i denotes a front properties can be achieved and cattle, respectively s, f. Space and time are not allowed,
you want to f out of the state in the past, so that the value of f properties and as asked for. I.e., becomes d [i] [s] = f of the form.
S represents a configuration attribute before use as cattle and f i and a maximum attribute for the case where the number of s. State transition from the two cases, namely with or without the current cattle.
dp [i] [j] = max (dp [i - 1] [j - s [i]] + f [i], dp [i - 1] [j]). The first dimension may be removed at the time of the actual operation, the optimization space.
However, since the value of s [i] is positive or negative, so in order to fill in the array to be determined based on the value s [i], and
if it is regular right to left (similar space 01 backpack optimization), if it is negative then from left to right.
Note: Dynamic Programming in the state of Victoria and the value can be transformed into each other. Victoria state too much, inefficient time,
can be converted to an array value; Similarly, when values are not the only array can not be planned, can increase the state-dimensional state in more detail

 


 

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
 5 #define per(i,j,k) for(int i = (j); i >= (k); i--)
 6 #define mv (int)1e5
 7 #define N 105
 8 #define inf (1LL << 30) - 1
 9 #define maxn (int)2e5 + 10
10 
11 int dp[maxn];
12 int s[N], f[N];
13 int n;
14 
15 void input(){
16 
17     rep(i, 0, maxn - 1) dp[i] = -inf;
18 
19     cin >> n;
20     rep(i, 1, n) cin >> s[i] >> f[i];
21 }
22 
23 void work(){
24 
25     dp[0 + mv] = 0;
26 
27     rep(i, 1, n){
28         if (s[i] > 0){
29             per(o, (int)1e5, (int)(-1e5 + s[i])) 
30                 dp[o + mv] = max(dp[o + mv], dp[o - s[i] + mv] + f[i]);
31         }
32         else{
33             rep(o, (int)-1e5, (int)(1e5 + s[i]))
34                 dp[o + mv] = max(dp[o + mv], dp[o - s[i] + mv] + f[i]);
35         }
36     }
37 
38     int ans = 0;
39     rep(i, 0, (int)1e5){
40         if (dp[i + mv] >= 0) ans = max(ans, i + dp[i + mv]);
41     }
42 
43     cout << ans << endl;
44 }
45 
46 int main(){
47 
48     ios::sync_with_stdio(false);
49     cin.tie(0);
50     input();
51     work();
52 
53     return 0;
54 }

 

Guess you like

Origin www.cnblogs.com/SSummerZzz/p/11163946.html