- Topic Address: D - Poisonous Full-Course
Topic to the effect
Time limit: 2 seconds, space limit: 1024MB, score value: 400 points
Problem Description
Xiao Ming came to a restaurant, there are a total of dishes, the first dish has the following attributes:
- If , then this dish is non-toxic, and if you eat it, you will get delicious value
- If , then this dish is poisonous, but eating it will also get delicious value
Initially, Xiao Ming's stomach was healthy. When Xiao Ming eats a dish, he will:
- when his stomach is healthy
- If the dish is non-toxic, his stomach is still healthy
- If the dish is poisonous, he will get a stomach bug
- when he got a stomach bug
- If the dish is non-toxic, his stomach will be healthy
- If the dish is poisonous, he will die
Now, the first dish will be displayed in front of Xiao Ming one by one , ... until the first dish is displayed. For each dish, Xiao Ming can choose to eat or not to eat. If you choose to eat, you may change the state and increase (decrease) the delicious value; if you choose not to eat, neither the state nor the delicious value will change.
Now, we want Xiao Ming to walk out of the restaurant alive. What is the maximum delicious value that Xiao Ming can get?
data size
- each value is an integer
- (In other words, either 0 or 1)
enter
Input comes from standard input, which has the following format:
⋮
output
Output an integer representing the answer.
Input sample 1
5
1 100
1 300
0 -200
1 500
1 300
output sample 1
600
Example explanation:
The following choices allow Xiao Ming to get the maximum delicious value (ie 600) without dying:
- He chooses to skip the first course, and now his stomach is healthy, and he gets a total of 0 delicious points;
- He chooses to enjoy the second course, and now his stomach is not healthy, and he has obtained a total of 300 delicious points;
- He chooses to enjoy the third course, and now his stomach is healthy, and he has gained a total of 100 delicious points;
- He chooses to enjoy the fourth course, and now his stomach is not healthy, and he has obtained a total of 600 delicious points;
- He chose to skip the fifth course, now his stomach is not healthy;
- In the end he got 600 delicious points and walked out of the restaurant alive.
Input sample 2
4
0 -1
1 -2
0 -3
1 -4
Output sample 2
0
In this set of examples, the maximum delicacy he can get is 0.
Input sample 3
15
1 900000000
0 600000000
1 -300000000
0 -700000000
1 200000000
1 300000000
0 -600000000
1 -900000000
1 600000000
1 -100000000
1 -400000000
0 900000000
0 200000000
1 -500000000
1 900000000
Output sample 3
4100000000
The answer doesn't have to be a 32-bit integer.
This question is obviously a dp.
If we define dp[i][0], it means that the stomach is still healthy after eating the first i dishes, and the maximum deliciousness obtained; dp[i][1] means that the stomach is sick after eating the first i dishes, and the maximum deliciousness is obtained value. So:
dp[i][0] has four cases: 0, dp[i-1][0]+y[i], dp[i-1][1]+y[i] (premise: x[i] ==0), dp[i-1][0]
dp[i][1] has only three cases: 0, dp[i-1][0]+y (premise: x[i]==1), dp[i-1][1]
In the end, the maximum delicious value that can be obtained is max(dp[n][0],dp[n][1]).
#include <iostream>
using namespace std;
typedef long long int ll;
ll n, dp[300005][2];
int main() {
cin >> n;
ll x, y;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
if (x == 0) {
dp[i][0] = max(dp[i][0], max(dp[i - 1][0] + y, dp[i - 1][1] + y));
}
else {
dp[i][1] = max(dp[i][1], dp[i - 1][0] + y);
}
dp[i][0] = max(dp[i][0], dp[i - 1][0]);
dp[i][1] = max(dp[i][1], dp[i - 1][1]);
}
cout << max(dp[n][0], dp[n][1]) << endl;
return 0;
}
Of course, this code can also optimize the space complexity:
#include <iostream>
using namespace std;
typedef long long int ll;
ll n;
int main() {
cin >> n;
ll p0 = 0, p1 = 0;
ll x, y;
for (int i = 1; i <= n; ++i) {
cin >> x >> y;
if (x == 0) {
p0 = max(p0, max(p0 + y, p1 + y));
}
else {
p1 = max(p1, p0 + y);
}
}
cout << max(p1, p0) << endl;
}
Easy to do!