Submission #1340517

#TimeUsernameProblemLanguageResultExecution timeMemory
1340517kokokaiMonochrome Points (JOI20_monochrome)C++20
100 / 100
13 ms7128 KiB
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int main() {
    // Tối ưu I/O trong C++
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    long long n;
    if (!(cin >> n)) return 0;
    string s;
    cin >> s;

    // Mảng lưu trữ chênh lệch (1-indexed)
    vector<long long> pre(2 * n + 1, 0);
    for (int i = 0; i < 2 * n; ++i) {
        if (s[i] == 'B') {
            pre[i + 1]++;
        } else {
            // Với điểm trắng, ta đánh dấu trừ ở vị trí đối diện (cách n bước)
            int target = (i + n) % (2 * n) + 1;
            pre[target]--;
        }
    }

    // Tính mảng cộng dồn
    for (int i = 1; i <= 2 * n; ++i) {
        pre[i] += pre[i - 1];
    }

    // Sao chép mảng cộng dồn ra để tìm trung vị
    vector<long long> sorted_pre(pre.begin() + 1, pre.end());
    sort(sorted_pre.begin(), sorted_pre.end());

    // Trung vị của mảng nằm ở vị trí thứ n (do mảng 0-indexed nên là n-1)
    long long median = sorted_pre[n - 1]; 

    // Tính toán kết quả: Tổng số cặp tối đa trừ đi tổng khoảng cách tới trung vị
    long long max_intersections = n * (n - 1);
    for (int i = 0; i < 2 * n; ++i) {
        max_intersections -= abs(sorted_pre[i] - median);
    }

    // In ra số lượng giao điểm tối đa
    cout << max_intersections / 2 << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...