ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #624 (Div. 3) (A~D,CD Good)

2021-08-26 17:05:31  阅读:218  来源: 互联网

标签:cnt Good cout 624 int cin Codeforces ++ abs


比赛链接:Here

1311A. Add Odd or Subtract Even

签到题,

  • \(a > b\) 时必须做做减法,如果差值为偶数的话只需要 \(1\) 次不然做一次减法后再做一次 \(+1\) 即可
  • \(a < b\) 同理了
  • \(a = b\) 0次
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        ll a, b;
        cin >> a >> b;
        if (a == b) cout << "0\n";
        else if (a > b) {
            if ((a - b) & 1) cout << "2\n";
            else cout << "1\n";
        } else {
            if ((a - b) & 1) cout << "1\n";
            else cout << "2\n";
        }
    }
}

1311B. WeirdSort

题意:给出长度为 \(n\)​ 的数组以及长度为 \(m\)​ 的允许进行 \(swap(a[i],a[i + 1])\) 的下标,

问经过若干次之后是否能使得数组有序

思路:

注意到 \(n\le 100\) 那么我可以直接跑若干次循环对于 \(a[i] < a[i + 1]\) 的地方交换 (前提是允许交换),

如果最后有序了就输出 YES

const int N = 110;
int a[N], st[N];
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n, m;
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> a[i];
        memset(st, 0, sizeof(st));
        for (int i = 1, x; i <= m; ++i) cin >> x, st[x] = 1;
        for (int i = 0; i < n; ++i)
            for (int j = 1; j < n; ++j)
                if (a[j] > a[j + 1] and st[j]) swap(a[j], a[j + 1]);
        bool f = 1;
        for (int i = 1; i < n and f; ++i) if (a[i] > a[i + 1]) f =  0;
        cout << (f ? "YES\n" : "NO\n");
    }
}

1311C. Perform the Combo

题意:给一个长度为n的字符串 会犯m个错误 每次犯错误就要重新输入

问你所有字母共打了多少次?

思路

似乎很多人用前缀和写的,

我的思路是在某个位置去二分找是否有若干次在它之后会出错的次数并累计即可。

int cnt[300];
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        memset(cnt, 0, sizeof(cnt));
        int n, m;
        string s;
        cin >> n >> m >> s;
        vector<int>p;
        for (int i = 0, x; i < m; ++i) {
            cin >> x;
            p.emplace_back(x);
        }
        sort(p.begin(), p.end());
        for (int i = 0; i < n; ++i) {
            int t = p.end() - lower_bound(p.begin(), p.end(), i + 1);
            cnt[s[i]] += 1 + t;
            // cout << s[i] << ": " << t << "\n";
        }
        for (auto c = 'a'; c <= 'z'; ++c) cout << cnt[c] << " ";
        cout << "\n";
    }
}

1311D. Three Integers

题意:

给了\(a、b、c\) 三个数,现在你可以对任意一个数进行任意次数的 \(+1\) 和 \(-1\) 问你最少操作次数让\(b\%a=0,c\%b=0\)

思路:

那 \(a,b,c\) 改变的最大范围也就是 \([1,10000]\),直接枚举就可以,但是 \(10000^3\)​ 明显是会超时的。这里稍微优化一下。

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int a, b, c;
        cin >> a >> b >> c;
        int aa = a, bb = b, cc = c;
        int cnt = INT_MAX;
        for (int i = 1; i <= 11000; ++i)
            for (int j = 1; i * j <= 11000; ++j)
                for (int k = 1; i * j * k <= 11000; ++k) {
                    if (cnt > abs(i - a) + abs(i * j - b) + abs(i * j * k - c)) {
                        cnt = abs(i - a) + abs(i * j - b) + abs(i * j * k - c);
                        aa = i, bb = i * j, cc = i * j * k;
                    }
                }
        cout << cnt << "\n" << aa << " " << bb << " " << cc << "\n";
    }
}

标签:cnt,Good,cout,624,int,cin,Codeforces,++,abs
来源: https://www.cnblogs.com/RioTian/p/15190470.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有