optimize_ofast's blog

By optimize_ofast, history, 3 hours ago, In English

Before starting the main topic, I would like to declare that the solution only includes the MX-J10 competition.

Also, don't criticize if you don't like it. Thank you for your support and we hope you can provide valuable advice.

Q1.【MX-X8-T0】「TAOI-3」Scores

Approach: Calculate the total score for the first correction: Add up all the $$$a_i$$$ to obtain the total score for the first correction. Determine whether re grading is necessary: If the total score of the first grading is greater than or equal to the passing score $$$T$$$, a second grading is required. Otherwise, the total score of the first grading will be directly output. Calculate the total score for the second round of correction: If a re correction is required, add up all the $$$b_i$$$ values to obtain the total score for the second round of correction.Output the final total score: Based on whether it needs to be re evaluated, output the corresponding total score.

Code:

#include <iostream>
using namespace std;

int main() {
    int n, T;
    cin >> n >> T;

    int a[n], b[n];
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
    }
    int sum_a = 0;
    for (int i = 0; i < n; ++i) {
        sum_a += a[i];
    }
    if (sum_a >= T) {
        int sum_b = 0;
        for (int i = 0; i < n; ++i) {
            sum_b += b[i];
        }
        cout << sum_b << endl;
    } else {
        cout << sum_a << endl;
    }

    return 0;
}

Q2. 【MX-X8-T1】「TAOI-3」Lucky clover

To solve this problem, we need to find a way to maximize the sum of the entire sequence through at least one operation (replacing all the numbers in a certain interval with $$$x$$$). Here are the solutions:

Solution approach: Initial sum: First, calculate the initial sum of the sequence.

The impact of replacing intervals: For any interval $$$[l, r]$$$, if the numbers in this interval are replaced with x, the new sum can be expressed as:

$$$new_sum = sum - (a[l] + a[l+1] + ... + a[r]) + (r - l + 1) * x$$$

That is to say, the new sum is equal to the original sum minus the original number in the interval, plus the replaced number in the interval.

Maximizing the new sum: We need to find an interval $$$[l, r]$$$ that maximizes new_stum. This is equivalent to finding an interval that maximizes $$$(r - l+1) * x - (a[l]+a[l+1]+...+a[r])$$$.

Transform into the largest sub segment and problem: We can transform the problem into finding an interval $$$[l, r]$$$ that maximizes $$$(x-a[l])+(x-a[l+1])+...+(x-a[r])$$$. This is actually a classic maximum subsegment and problem, except that the value of each element is $$$x-a[i]$$$.

Dynamic programming solution: Use dynamic programming (Kadane algorithm) to solve the maximum subsegment and problem.

Code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, x;
    cin >> n >> x;

    vector<int> a(n);
    long long sum = 0; 
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }

    vector<long long> d(n);
    for (int i = 0; i < n; ++i) {
        d[i] = x - a[i];
    }

    long long b = 0; 
    long long c = 0;
    for (int i = 0; i < n; ++i) {
        c += d[i];
        if (c < 0) c = 0;
        if (c > b) b = c;
    }

    long long result = sum + b;
    cout << result << endl;

    return 0;
}

That's all. And thank you for your patience to read this passage.

Although these two questions' solutions which I shared with you might be a bit easy, but I think that the most important thing is still to support and give me a "like".

THANKS AGAIN.

By optimize_ofast.

Beijing Time: 2025.02.01 12:47

  • Vote: I like it
  • -7
  • Vote: I do not like it