Toyota Programming Contest 2023#3 (AtCoder Beginner Contest 306) The fourth question of the competition on June 17


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 Ndishes, the first idish has the following attributes:

  • If X_{i}= 0, then this dish is non-toxic, and if you eat it, you will get Y_{i}delicious value
  • If X_{i}= 1, then this dish is poisonous, but eating it will also get Y_{i}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

1Now, the first dish will be displayed in front of Xiao Ming one by one , ... until the first Ndish 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
  • 1\leq N\leq 3\times 10^{5}
  • X_{i}\in \left \{ 0,1\right \} (In other words, x_{i}either 0 or 1)
  • -10^9 \leq Y_i \leq 10^9

enter

Input comes from standard input, which has the following format:

N

X_1   Y_1

X_2   Y_2

X_N   Y_N

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!

Guess you like

Origin blog.csdn.net/qq_43546083/article/details/131320364