CF525 ABCDE, cf525abcde

Source: Internet
Author: User

CF525 ABCDE, cf525abcde

XGG Blog

  • CF525A Vitaliy and Pie
  • CF525B Pasha and String
  • CF525C Ilya and Sticks
  • CF525D Arthur and Wils
  • CF525E Anya and Cubes
SolutionsCF525A Vitaliy and Pie

Brute force.

CF525B Pasha and String

The effect will be eliminated if the accumulated times of reverse is even.

CF525C Ilya and Sticks

Greedy.

CF525D Arthur and Wils

The wall shoshould be broken if there is 2 × 2 Sub-matrix contains only one*.

CF525E Anya and Cubes

DivideaInto two parts to avoid the exponential explosion. The time complexity is about O (3n + 12 commit log (3n + 12) Commit k) .

Accepted CodesCF525A Vitaliy and Pie
#include <bits/stdc++.h>using namespace std;int main() {    int n;    string s;    while (cin >> n >> s) {        int ans = 0;        map<int, int> keys;        for (char c : s) {            if ('a' <= c && c <= 'z') {                ++keys[c - 'a' + 'A'];            } else {                if (keys[c] > 0) {                    --keys[c];                } else {                    ++ans;                }            }        }        cout << ans << endl;    }    return 0;}
CF525B Pasha and String
#include <bits/stdc++.h>using namespace std;int main() {    int n, m, a;    string s;    while (cin >> s) {        n = s.length();        vector<bool> v(n, false);        cin >> m;        while (m--) {            cin >> a;            v[a - 1] = v[a - 1] ^ true;            v[n - a] = v[n - a] ^ true;        }        bool rev = false;        for (int i = 0; i < n / 2; ++i) {            rev ^= v[i];            if (rev) {                swap(s[i], s[n - i - 1]);            }        }        cout << s << endl;    }    return 0;}
CF525C Ilya and Sticks
#include <bits/stdc++.h>using namespace std;int main() {    int n;    while (cin >> n) {        vector<int> l(n);        for (int i = 0; i < n; ++i) {            cin >> l[i];        }        sort(l.begin(), l.end());        int first = 0;        long long ans = 0;        for (int i = n - 1; i >= 0; --i) {            if (l[i] == l[i - 1] || l[i] - 1 == l[i - 1]) {                if (first == 0) {                    first = l[i - 1];                    --i;                } else {                    ans += 1LL * first * l[i - 1];                    --i;                    first = 0;                }            }        }        cout << ans << endl;    }    return 0;}
CF525D Arthur and Wils
#include <bits/stdc++.h>using namespace std;const int STEP_X[] = {1, 1, -1, -1};const int STEP_Y[] = {1, -1, 1, -1};void dfs(vector<string> &apart, int x, int y) {    for (int i = 0; i < 4; ++i) {        int tx = x + STEP_X[i];        int ty = y + STEP_Y[i];        if (tx >= 0 && tx < apart.size()) {            if (ty >= 0 && ty < apart[x].size()) {                if (apart[tx][ty] == '*' && apart[tx][y] == '.' && apart[x][ty] == '.') {                    apart[tx][ty] = '.';                    dfs(apart, tx, ty);                } else if (apart[tx][ty] == '.' && apart[tx][y] == '*' && apart[x][ty] == '.') {                    apart[tx][y] = '.';                    dfs(apart, tx, y);                } else if (apart[tx][ty] == '.' && apart[tx][y] == '.' && apart[x][ty] == '*') {                    apart[x][ty] = '.';                    dfs(apart, x, ty);                }            }        }    }}int main() {    int n, m;    while (cin >> n >> m) {        vector<string> apart(n);        for (int i = 0; i < n; ++i) {            cin >> apart[i];        }        for (int i = 0; i < n; ++i) {            for (int j = 0; j < m; ++j) {                if (apart[i][j] == '.') {                    dfs(apart, i, j);                }            }        }        for (int i = 0; i < n; ++i) {            cout << apart[i] << endl;        }    }    return 0;}
CF525E Anya and Cubes
#include <bits/stdc++.h>using namespace std;long long expo[20];void dfs(vector<long long> &a,         map<long long, map<int, int>> &num,         int index,         long long sum,         int stickNum,         int k) {    if (index == a.size()) {        ++num[sum][stickNum];        return;    }    dfs(a, num, index + 1, sum, stickNum, k);    dfs(a, num, index + 1, sum + a[index], stickNum, k);    if (a[index] < 20 && stickNum < k) {        dfs(a, num, index + 1, sum + expo[a[index]], stickNum + 1, k);    }}map<long long, map<int, int>> getCombination(vector<long long> &a, int k) {    map<long long, map<int, int>> num;    dfs(a, num, 0, 0, 0, k);    return num;}int main() {    int n, k;    long long S;    expo[0] = 1;    for (int i = 1; i < 20; ++i) {        expo[i] = i * expo[i - 1];    }    while (cin >> n >> k >> S) {        vector<long long> a(n >> 1);        vector<long long> b(n - (n >> 1));        for (int i = 0; i < (n >> 1); ++i) {            cin >> a[i];        }        for (int i = (n >> 1); i < n; ++i) {            cin >> b[i - (n >> 1)];        }        auto numA = getCombination(a, k);        auto numB = getCombination(b, k);        long long ans = 0;        for (auto sum : numA) {            for (auto it : sum.second) {                for (int i = 0; i <= k - it.first; ++i) {                    ans += 1LL * it.second * numB[S - sum.first][i];                }            }        }        cout << ans << endl;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.