Codeforces Round #719 (Div. 3) A~E题解

51鸽了几天,有几场比赛的题解还没发布,今天晚上会补上的

1520A. Do Not Be Distracted!

问题分析

模拟,如果存在已经出现的连续字母段则输出NO

using ll = long long;
void solve() {
    int n;
    string s;
    cin >> n >> s;
    bool vis[30] = {false};
    for (int i = 0; i < n; ++i) {
        if (vis[s[i] - 'A']) {
            cout << "NO\n";
            return;
        }
        int j = i;
        while (s[j] == s[i]) j++;
        vis[s[i] - 'A'] = true;
        i               = j;
    }
    cout << "YES\n";
}

1520B. Ordinary Numbers

using ll = long long;
bool check(int x) {
    string temp = to_string(x);
    for (int i = 0; i < temp.size() - 1; i++)
        if (temp[i] != temp[i + 1]) return false;
    return true;
}
ll ans;
void solve() {
    ll n;
    cin >> n, ans = 0;
    int k = 1, temp = 1;
    for (int i = temp; i <= n; i += temp) {
        if (check(i)) ans++;
        else {
            temp = temp * 10 + 1;
            i    = 0;
        }
    }
    cout << ans << endl;
}

1520C. Not Adjacent Matrix

问题分析:构造思想

  • \(n = 2\) ,无论怎么构造矩阵都会产生相邻的矩阵。
  • 其他情况,以 \(1\) 为起点每次间隔 + 2,因为 数值不超过 \(n * n\) 所以,在大于此值时再从 \(2\) 开始枚举,这样一定能填充整个 \(n * n\) 矩阵
void solve() {
    int n;
    cin >> n;
    vector<int> a(100 * 100 + 10);
    if (n == 2) {
        cout << -1 << "\n";
        return;
    }
    int cnt = 1;
    for (int i = 1; i <= n * n; ++i) {
        if (cnt <= n * n) a[i] = cnt, cnt += 2;
        if (cnt > n * n) cnt = 2;
    }
    for (int i = 1; i <= n * n; ++i) {
        cout << a[i];
        if (i % n == 0) cout << "\n";
        else
            cout << " ";
    }
}

1520D. Same Differences

问题分析

变换公式,\(a_j - a_i = j - i \to a_j - j = a_i - i\)

所以我们可以存储 \(a_x - x\) 的值,然后进行组合数计算 \(C_m^2\)\(m\) 代表 \(a_x - x\) 的个数

using ll = long long;
void solve() {
    int n;
    map<int, ll> mp;
    cin >> n;
    for (ll i = 1, x; i <= n; ++i) {
        cin >> x;
        mp[x - i]++;
    }
    ll cnt = 0;
    for (auto p : mp) cnt += p.second * (p.second - 1) / 2;
    cout << cnt << "\n";
}

1520E. Arranging The Sheep

问题分析:贪心

对于绵羊序列,两端都往中间移动一定最优

void solve() {
    int n;
    string s;
    cin >> n >> s;
    vector<int> a;
    int empty = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '.') empty++;
        else
            a.push_back(empty);
    }
    int mid = (a.size() - 1) >> 1;
    ll ans  = 0;
    for (auto x : a) ans += abs(x - a[mid]);
    cout << ans << "\n";
}

1520F1. Guess the K-th Zero (Easy version)

// 待补
posted @ 2021-05-06 08:57  RioTian  阅读(128)  评论(0编辑  收藏  举报