NowCoder Weekly Contest Round 51 Solutions

Problem A: Simple Calculation

Given an integer m, output the ceiling of m/2.

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int64 m;
    cin >> m;
    cout << (m + 1) / 2 << '\n';
    return 0;
}

Problem B: Digit Sum Divisibility

Read n strings, calcultae the sum of all digits, and check if divisible by 3.

#include <bits/stdc++.h>
#include <string>
using namespace std;
using int64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    int64 total = 0;
    
    for (int i = 0; i < n; i++) {
        string str;
        cin >> str;
        for (char c : str) {
            total += c - '0';
        }
    }
    
    cout << (total % 3 == 0 ? "YES" : "NO") << '\n';
    return 0;
}

Problem C: Time Calculation

Calculate minimum time based on different conditions.

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    double current, speed, target, acc1, acc2, acc3;
    cin >> current >> speed >> target >> acc1 >> acc2 >> acc3;
    
    double result;
    if (current <= target) {
        result = (100.0 - current) / acc3;
    } else {
        double option1 = (100.0 - current) / acc2;
        double option2 = (current - target) / speed + (100.0 - target) / acc3;
        result = min(option1, option2);
    }
    
    cout << fixed << setprecision(9) << result << '\n';
    return 0;
}

Problem D: String Modulo and GCD

Calculate GCD(b, f(a) mod b) where f(a) converts string a to number.

#include <bits/stdc++.h>
#include <string>
using namespace std;
using int64 = long long;

int64 computeGCD(int64 x, int64 y) {
    return y == 0 ? x : computeGCD(y, x % y);
}

int64 computeModulo(const string& num, int64 mod) {
    int64 result = 0;
    for (char ch : num) {
        result = (result * 10 + (ch - '0')) % mod;
    }
    return result;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string str;
    int64 b;
    cin >> str >> b;
    
    int64 remainder = computeModulo(str, b);
    cout << computeGCD(b, remainder) << '\n';
    
    return 0;
}

Problem E: Minimum Maximum Value Path

Find the minimum possible maximum value along a path from (1,1) to (n,n) moving in four directoins.

Approach: Binary search on answer + BFS verification.

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

const int MAXN = 505;
int64 grid[MAXN][MAXN];
bool visited[MAXN][MAXN];

struct Cell {
    int x, y;
};

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

bool canReach(int n, int64 limit) {
    if (grid[1][1] > limit) return false;
    
    memset(visited, false, sizeof(visited));
    queue<Cell> q;
    q.push({1, 1});
    visited[1][1] = true;
    
    while (!q.empty()) {
        Cell cur = q.front();
        q.pop();
        
        if (cur.x == n && cur.y == n) return true;
        
        for (int dir = 0; dir < 4; dir++) {
            int nx = cur.x + dx[dir];
            int ny = cur.y + dy[dir];
            
            if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
            if (visited[nx][ny]) continue;
            if (grid[nx][ny] > limit) continue;
            
            visited[nx][ny] = true;
            q.push({nx, ny});
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> grid[i][j];
        }
    }
    
    int64 low = 0, high = 1e9;
    while (low < high) {
        int64 mid = (low + high) / 2;
        if (canReach(n, mid)) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }
    
    cout << low << '\n';
    return 0;
}

Tags: Nowcoder contest algorithm C++ Binary Search

Posted on Wed, 26 Aug 2026 16:21:38 +0000 by koray