Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Contest Solutions: NiuKe Beginner Monthly Contest 98 and ABC362

Tech 1

Problem A - Dice Magic (NiuKe Beginner Monthly Contest 98)

Check if a target number exists within a given array.

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

long long n, x;
long long arr[505];

void solve() {
    for (int i = 1; i <= n; ++i) {
        cin >> arr[i];
    }
    
    for (int i = 1; i <= n; ++i) {
        if (arr[i] == x) {
            cout << "YES" << endl;
            return;
        }
    }
    
    cout << "NO" << endl;
}

int main() {
    cin >> n >> x;
    solve();
    return 0;
}

Problem B - Minimum Remaining Count (NiuKe Beginner Monthly Contest 98)

Determine the minimal count of numbers left after pairing odd and even numbers based on their sums and products.

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

long long n;
long long nums[1000010];

void solve() {
    long long odd_count = 0, even_count = 0;
    
    for (int i = 1; i <= n; ++i) {
        if (nums[i] % 2)
            odd_count++;
        else
            even_count++;
    }
    
    if (even_count > odd_count)
        cout << even_count - odd_count << endl;
    else {
        if ((odd_count - even_count) % 2)
            cout << "1" << endl;
        else
            cout << "0" << endl;
    }
}

int main() {
    cin >> n;
    solve();
    return 0;
}

Problem C - Two Functions (NiuKe Beginner Monthly Contest 98)

Compute the result of a mathematical function involving modular arithmetic.

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

const int MOD = 998244353;
long long a, x;

void solve() {
    if (x == 1) {
        cout << (a % MOD) << endl;
        return;
    }
    
    long long term1 = (a * a) % MOD;
    long long term2 = (((x * x) - x) / 2) % MOD;
    long long result = (term1 * term2) % MOD;
    
    cout << result << endl;
}

int main() {
    int q;
    cin >> q;
    while (q--) {
        cin >> a >> x;
        solve();
    }
    return 0;
}

Problem A - Buy a Pen (AtCoder)

Determine the minimum cost among two options based on the first character of a string.

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

void solve() {
    int red, green, blue;
    cin >> red >> green >> blue;
    
    string color;
    cin >> color;
    
    char first_char = color[0];
    
    if (first_char == 'R') {
        cout << min(green, blue) << endl;
    } else if (first_char == 'B') {
        cout << min(red, green) << endl;
    } else {
        cout << min(red, blue) << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}

Problem B - Right Triangle (AtCoder)

Verify if three points form a right triangle using distance calculations.

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

void solve() {
    int x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    
    // Check collinearity
    if ((x1 - x2) * (y2 - y3) == (x2 - x3) * (y1 - y2)) {
        cout << "No" << endl;
        return;
    }
    
    long long side1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
    long long side2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
    long long side3 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
    
    if (side1 + side2 == side3 || side2 + side3 == side1 || side1 + side3 == side2) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}

Problem C - Sum Equals Zero (AtCoder)

Determine if it's possible to select values from intervals such that their total sum equals zero.

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

const int MAX_N = 200010;
int left_bounds[MAX_N];
int right_bounds[MAX_N];
int result[MAX_N];

void solve() {
    int num_intervals;
    cin >> num_intervals;
    
    long long total_left = 0;
    long long total_right = 0;
    
    for (int i = 1; i <= num_intervals; ++i) {
        cin >> left_bounds[i] >> right_bounds[i];
        total_left += left_bounds[i];
        total_right += right_bounds[i];
    }
    
    if (total_left <= 0 && total_right >= 0) {
        cout << "Yes" << endl;
        long long current_sum = 0;
        
        for (int i = 1; i <= num_intervals; ++i) {
            result[i] = left_bounds[i];
            current_sum += left_bounds[i];
        }
        
        for (int i = 1; i <= num_intervals; ++i) {
            long long diff = right_bounds[i] - left_bounds[i];
            if (current_sum + diff < 0) {
                current_sum += diff;
                result[i] = right_bounds[i];
            } else {
                result[i] += (0 - current_sum);
                current_sum = 0;
                break;
            }
        }
        
        for (int i = 1; i <= num_intervals; ++i) {
            cout << result[i] << " ";
        }
        cout << endl;
    } else {
        cout << "No" << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
Tags: algorithm

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.