Educational Codeforces Round 161 (Rated for Div. 2) | JorbanS

A - Tricky Template

string solve() {
    cin >> n >> a >> b >> c;
    if (a == c || b == c) return no;
    for (int i = 0; i < n; i ++) {
        if (a[i] == b[i]) {
            if (a[i] != c[i]) return yes;
        } else {
            if (a[i] != c[i] && b[i] != c[i]) return yes;
        }
    }
    return no;
}

B - Forming Triangles

ll C2(ll x) { return x * (x - 1) / 2; }
ll C3(ll x) { return x * (x - 1) * (x - 2) / 6; }
 
ll solve() {
    cin >> n;
    map<int, int> mp;
    for (int i = 0; i < n; i ++) {
        int x; cin >> x;
        mp[x] ++;
    }
    ll res = 0, cnt = 0;
    for (auto [x, y]: mp) {
        if (y >= 2) res += C2(y) * cnt;
        if (y >= 3) res += C3(y);
        cnt += y;
    }
    return res;
}

C - Closest Cities

前缀和

int n, m;
int a[N];
int l[N], r[N];
int L[N], R[N];
 
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    L[n + 1] = 0;
    r[1] = l[n] = 1;
    for (int i = 2; i <= n - 1; i ++) {
        l[i] = abs(a[i] - a[i - 1]);
        r[i] = abs(a[i] - a[i + 1]);
        if (l[i] < r[i]) l[i] = 1;
        else r[i] = 1;
    }
    for (int i = 1; i <= n; i ++) R[i] = R[i - 1] + r[i];
    for (int i = n; i >= 1; i --) L[i] = L[i + 1] + l[i];
    cin >> m;
    while (m --) {
        int x, y; cin >> x >> y;
        if (x < y) cout << R[y - 1] - R[x - 1] << endl;
        else cout << L[y + 1] - L[x + 1] << endl;
    }
}

你可能感兴趣的:(OI,题解,算法,c++)