Codeforces Round 891 (Div. 3)

A. Array Coloring

//判断奇数出现的个数即可



void solve(){
    int n; cin >> n;
    int cnt = 0;
    for (int i = 0, t; i < n; ++i){
        cin >> t;
        cnt += t & 1;
    }
    cout << (cnt & 1 ? no : yes);
}

B. Maximum Rounding

//找到第一个进位后按加法余数进位方式进一遍即可


void solve(){
    string s; cin >> s;
    string result;
    bl ok = false;
    for (int i = 0; i < sz(s); ++i){
        if (ok){result.pb('0'); continue;}
        result.pb(s[i]);
        if (s[i] >= '5'){
            int rem = 0;
            for (int j = sz(result) - 1; j >= 0; --j){
                result[j] += rem;
                if (result[j] >= '5'){
                    result[j] = '0';
                    rem = 1;
                }
                else{
                    rem = 0;
                }
            }
            if (rem){result = "1" + result;}
            ok = true;
        }
    }
    cout << result << "\n";
}

C. Assembly via Minimums

//贪心地从小到达依此构造每个数,贪心思路是当前数组下标左边的数均比它小,所以待构造数组右边的数字个数就是当前构造的数组下标的数字出现的次数,哈希统计出现次数后贪心地依此构造即可,题目保证有答案就不需考虑其他因素。

 

void solve(){
    int n; cin >> n;
    int m = (n - 1) * n / 2;
    map mapp;
    for (int i = 0, t; i < m; ++i){
        cin >> t; mapp[t] ++;
    }
    vi result(n + 1);
    int i = 1;
    liter(x, mapp){
        while (x.second > 0){
            x.second -= (n - i);
            result[i ++] = x.first;
        }
    }
    result[n] = result[n - 1];
    for (i = 1; i <= n; ++i){
        cout << result[i] << " \n"[i == n];
    }
}
 

D. Strong Vertices

只想得到BF, 寄

E. Power of Points

//枚举所有区间必然TLE,可以想到在枚举每个区间端点s时,将区间分为以s为左端点的区间和以s为右端点的区间,再根据前缀和数组以及被s划分的左右区间的数量可以O(1)的计算出当前端点s对最后答案的贡献。

void solve(){
    int n; cin >> n;
    vi a(n); liter(x, a) cin >> x;
    auto b = a;
    sort(all(a));
    a.insert(a.bg, 0);
    vll prefix(n + 1);
    for (int i = 1; i <= n; ++i){
        prefix[i] = prefix[i - 1] + a[i];
    }
    map mapp;
    for (int i = 1; i <= n; ++i){ll result = 0;
        if (i > 1 && a[i] == a[i - 1]) continue;
        int m = i, p = 1;
        ll bd = a[i];
        while (p){
            if (m + p <= n && a[m + p] <= a[i]){
                m += p; p *= 2;
            }
            else p /= 2;
        }
        int left = (m), right = (n - left);
        result = result + (1ll * a[i] * left - prefix[left] + left) + (prefix[n] - prefix[left] - 1ll * a[i] * right + right);
        mapp[a[i]] = result;
    }
    for (int i = 0; i < n; ++i){
        cout << mapp[b[i]] << " \n"[i == n - 1];
    }
}
 

F. Sum and Product

TLE了,复杂度应该是O(q) * O(n) * O(logn)

void solve(){
    int n; cin >> n;
    map mapp;
    for (int i = 0; i < n; ++ i){
        int t; cin >> t; mapp[t] ++;
    }
    int q; cin >> q;
    while (q--){
        ll x, y; cin >> x >> y;
        set sett;
        ll result = 0;
        liter(a, mapp){
            ll t = x - a.first;
            if (t == a.first && t * t == y){
                if (!sett.count(t))result += a.second * (a.second - 1) / 2;
                sett.insert(t);
                continue;
            }
            if (mapp.count(t) && t * a.first == y && !sett.count(t)){
                result += a.second * mapp[t];
                sett.insert(a.first);
                sett.insert(t);
            }
        }
        cout << result << " \n"[q == 0];
    }
}

你可能感兴趣的:(被CF虐待的日常,算法,java,数据结构)