optimize_ofast's blog

By optimize_ofast, history, 4 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

Full text and comments »

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

By optimize_ofast, history, 4 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

Full text and comments »

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

By optimize_ofast, history, 6 months ago, In English

Now, I am a student in a middle school. Programming is an important interest for me, and I think my programming skills are a litte poor. So could you give me some suggestions?

My English is not very good, sorry for now.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By optimize_ofast, history, 11 months ago, In English

Hello, codeforces!

Before starting to explain the topic, what I want to say is that in some of the solutions I have previously published, some highly skilled people have said that my solutions are "redundant", and they prefer to see the official solutions rather than the ones written by others. Perhaps there is a slight deviation in my understanding, but my level is indeed very limited. The solution I provided is my personal opinion, so I don't think it's unnecessary. Because my coding ability is not very high, I hope everyone can give me more encouragement, okay?

Today, I will provide the solution to question A in this competition.

This is just a easy string simulation. We just need to define a variable "flag" to determine if the different character has been found. Because the essence of a string is a character array, its minimum index is 0. So we need to add 1 to the number.

This is the AC Code:

#include <bits/stdc++.h>
using namespace std;

string s;
int main(){
    cin >> s;
    int n = s.length();
    for(int i = 0; i < n; i++) {
        bool flag = true;
        for(int j = 0; j < n; j++) {
            if(i != j and s[i] == s[j]) flag = false;
        }
        if(flag == true) cout << i + 1 << endl;
    }
}

Full text and comments »

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

By optimize_ofast, history, 11 months ago, In English

This question is quite easy!

We only need to input t sets of data and traverse each string. And use two counters, "suma" and "sumb", to determine the number of occurrences of letter A and letter B, and compare them.

It is worth noting that the title has already mentioned that the length of the string is 5, so we do not need to determine whether suma is equal to sumb.

Hope to see your comments!

Below is the AC code:

#include <bits/stdc++.h>
using namespace std;

int t;
int main() {
	cin >> t;
	while(t--) {
		string s;
		cin >> s;
		int suma = 0, sumb = 0;
		for(int i = 0; i < s.length(); i++) {
			if(s[i] == 'A') suma++;
			else sumb++;
		}
		if(suma > sumb) cout << "A" << endl;
		else cout << "B" << endl;
	}
	return 0;
}

Full text and comments »

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

By optimize_ofast, history, 11 months ago, In English

If there are "spikes" in two or more consecutive cells, then only the quantity of all previous coins needs to be counted; If there are no consecutive $2 $or more cells with spikes, it can be proven that we can definitely reach the last cell.

So this is the code:

#include <bits/stdc++.h>
using namespace std;

int sum = 0;
int main() {
    int t;
    cin >> t;
    while (t--) {
    	int n, now = 0;
    	string s;
    	cin >> n >> s;
    	int sum = 0;
        if(s.find("**") != string::npos) { 
        	for(int i = 0; i < s.find("**"); i++) {
        		if(s[i] == '@') sum++;
			}
			cout << sum << endl;
		}
		else {
			for(int i = 0; i < s.length(); i++) {
				if(s[i] == '@') sum++;
			}
			cout << sum << endl;
		}
    }
    return 0;
}

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it