Codeforces Round 871 (Div. 4) A ~ G

这篇具有很好参考价值的文章主要介绍了Codeforces Round 871 (Div. 4) A ~ G。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

A. Love Story

Problem - A - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

//const int N = 

void solve() {
    string s;
    cin >> s;
    int cnt = 0;
    string tmp = "codeforces";
    for (int i = 0; i < s.size(); i ++) 
        if (s[i] != tmp[i]) cnt ++;
    cout << cnt << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    while (t --) solve();
    
    return 0;
}

B. Blank Space

Problem - B - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

//const int N = 

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 10);
    for (int i = 1; i <= n; i ++) cin >> a[i];
    
    int ans = -inf, len = 0;
    for (int i = 1; i <= n; i ++) {
        if (a[i] == 0) {
            len ++;
        }
        if (a[i] != 0) {
            ans = max(ans, len);
            len = 0;
        }
    }
    cout << max(ans, len) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    
    while (t --) solve();

    return 0;
}

C. Mr. Perfectly Fine

Problem - C - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

//const int N = 

void solve() {
    int n;
    cin >> n;

    ll min1 = inf, min2 = inf, mintot = inf;
    for (int i = 1; i <= n; i ++) {
        ll a; string b;
        cin >> a >> b;
        if (b == "10") {
            if (min1 > a) {
                min1 = a;
            }
        } 
        if (b == "01") {
            if (min2 > a) {
                min2 = a;
            }
        }
        if (b == "11") {
            if (mintot > a) {
                mintot = a;
            }
        }
    }

    ll res = min1 + min2;
    res = min(mintot, res);

    if (res >= inf / 2) cout << "-1" << endl;
    else cout << res << endl;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    while(t --) solve();

    return 0;
}

D. Gold Rush     

Problem - D - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

//const int N = 

void solve() {
    int n, m;
    cin >> n >> m;
    
    if (n == m) cout << "YES" << endl;
    else if (n < m) cout << "NO" << endl;
    else {
        unordered_map<int, bool> mp;
        queue<int> q;
        q.push(m);
        while (q.size() != 0) {
            int t = q.front();
            q.pop();

            if (t % 2 == 0) {
                int tmp1 = t / 2 * 3;
                if (tmp1 <= n) {
                    q.push(tmp1);
                    mp[tmp1] = true;
                }

                int tmp2 = t * 3;
                if (tmp2 <= n) {
                    q.push(tmp2);
                    mp[tmp2] = true;
                }
            }
            else {
                int tmp2 = t * 3;
                if (tmp2 <= n) {
                    q.push(tmp2);
                    mp[tmp2] = true;
                }
            }
        }
        if (mp[n] == true) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    while (t --) solve();
    
    return 0;
}

E. The Lakes

Problem - E - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

const int N = 1010;
int n, m;
int g[N][N];
bool st[N][N];

int dirx[4] = {-1, 0, 1, 0}, diry[4] = {0, 1, 0, -1};

int bfs(int x, int y) {
    int ans = 0;
    queue<PII> q;

    st[x][y] = true;
    q.push({x, y});
    ans += g[x][y];

    while (q.size() != 0) {
        PII t = q.front();
        q.pop();

        for (int i = 0; i < 4; i ++) {
            int tx = t.first + dirx[i], ty = t.second + diry[i];
            if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && g[tx][ty] > 0 && st[tx][ty] == false) {
                st[tx][ty] = true;
                q.push({tx, ty});
                ans += g[tx][ty];
            }
        }
    }
    
    return ans;
}

void solve() {

    cin >> n >> m;

    for (int i = 1; i <= n; i ++) 
        for (int j = 1; j <= m; j ++) {
            cin >> g[i][j];
            st[i][j] = false;
        }
    
    int ans = 0;
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= m; j ++) {
            if (g[i][j] > 0 && st[i][j] == false) {
                ans = max(ans, bfs(i, j));
            }
        }
    }
    
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    while (t --) solve();
  
    return 0;
}

F. Forever Winter

Problem - F - Codeforces

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

// const int N = ;

void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> deg(n + 10);

    for (int i = 1; i <= m; i ++) {
        int u, v;
        cin >> u >> v;

        deg[u] ++; deg[v] ++;  // 统计每个点的度数
    }

    unordered_map<int, int> mp;
    for (int i = 1; i <= n; i ++) 
        if (deg[i] != 0) 
            mp[deg[i]] ++; // 统计不同度数出现的次数

    int ans1 = 0, ans2 = 0;
    for (auto x : mp) {
        if (x.second == 1) ans1 = x.first; // 出现次数为1, 则是中心点
        else {
            // 出现次数 >1, 且度数不是1, 即不是最外圈的点, 则是 y, 中间层的点
            if (x.first != 1) ans2 = x.first - 1; 
        }
    }
// 有可能中心点的度数和中间层的度数一样, 则没有出现次数为1的点, 则ans1 = 0, 
// 则中心点的度数 = 中间层点的度数 + 1
    if (ans1 == 0) ans1 = ans2 + 1;

    cout << ans1 << " " << ans2 << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    while (t --) solve();

    return 0;
}

G. Hits Different

Problem - G - Codeforces文章来源地址https://www.toymoban.com/news/detail-437282.html

// 1^2 + 2^2 + 3^2 + 4^2 + ... + n^2 = n * (n + 1) * (2 * n + 1) / 6
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
const int inf = 0x3f3f3f3f;
const ll infinf = 0x3f3f3f3f3f3f3f3f;

const int N = 2025; 
const int M = 2.1e6;

PII layer[N];
bool stl[M], str[M];
int n;
ll s[N][N];

int search(int x) {   // 查找 数x 在哪一层
    int now = 0;
    for (int i = 1; i <= x; i ++) {
        if (layer[i].first <= x && layer[i].second >= x) {
            now = i;
            break;
        }
    }
    return now;
}

ll call(int c, int now) {  // 计算左侧小三角的和
    ll ans = 0;
    for (int i = 1; i <= now - c + 1; i ++) { // 逐层利用公式O(1)时间复杂度求和并累加
        int l = layer[c + i - 1].first;
        int r = layer[c + i - 1].first + i - 1;
        ans += (ll)r * (r + 1) * (2 * r + 1) / 6 - (ll)(l - 1) * (l + 1 - 1) * (2 * l + 1 - 2) / 6;
    }
    return ans;
}

ll calr(int c, int now) {  // 计算右侧小三角的和
    ll ans = 0;
    for (int i = 1; i <= now - c + 1; i ++) {  // 逐层利用公式O(1)时间复杂度求和并累加
        int l = layer[c + i - 1].second - i + 1;
        int r = layer[c + i - 1].second;
        ans += (ll)r * (r + 1) * (2 * r + 1) / 6 - (ll)(l - 1) * (l + 1 - 1) * (2 * l + 1 - 2) / 6;
    }
    return ans;
}

void solve() {
    cin >> n;

    if (n == 1) { 
        cout << 1 << endl;
    }
    else if (n > 1) {
        int now = search(n);

        int pre = n - layer[now].first;  // n 前面有几个数
        int past = layer[now].second - n;    // n 后面有几个数

        int last = layer[now].second;  // n所在层的最后一个数是多少
        ll sum = (ll)last * (last + 1) * (2 * last + 1) / 6;  // 求大三角的总和
        int l = now - pre + 1;    // 左面小三角的第一个数是在第几层
        int r = now - past + 1;   // 右面小三角的第一个数是在第几层
        
        cout << sum - call(l, now) - calr(r, now) << endl; // 总大三角 - 左小三角 - 右小三角 = 中间所求的三角
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int l = 1, r = 1;    // 预处理出每一层的左右端点的数是多少
    layer[1] = {1, 1};
    for (int i = 2; i <= 2023; i ++) {
        l = layer[i - 1].first + i - 1;
        r = layer[i - 1].second + i;
        stl[l] = true; 
        str[r] = true;
        layer[i] = {l, r};
    }

    int t;
    cin >> t;

    while(t --) solve();

    return 0;
}

到了这里,关于Codeforces Round 871 (Div. 4) A ~ G的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Codeforces Round 894 (Div. 3)

    签到题 a数组里,大于等于前一个值的a[i]会被写到b里。直接遍历b,如果b[i]比前一个小就在它前面插一个相等的值 计算反过来的长度,判断是否相等就行 对于没有重复口味的集合,能选出的方案是n*(n-1)/2 (从n个里面选2个的组合数)。二分找出需要多少不同口味的冰淇淋,

    2024年02月11日
    浏览(14)
  • Codeforces Round 920 (Div. 3)

    Codeforces Round 920 (Div. 3)

    Codeforces Round 920 (Div. 3) 题意:随机给出正方形在平面坐标系上的四个顶点的坐标,求正方形的面积,正方形边与xy轴平行。 思路:因为正方形与坐标轴平行,所以找出相同的两x或y坐标即可找到一条边的长度,平方就是面积,注意结果返回整型。 AC code: 题意:给出两个01字符

    2024年01月17日
    浏览(31)
  • Codeforces Round 874 (Div. 3)

    Codeforces Round 874 (Div. 3)

    用最少的长度为2的字符串按一定规则拼出s。规则是:前一个字符串的尾与后一个字符串的首相同。 统计s中长度为2的不同字符串数量。 给定数组a[n]和b[n],重排b后,令任意|ai−bi|≤k成立(1≤k≤n)数据保证一定有解。 将a和b分别按从小到大的顺序匹配便是最优的,一定能满足

    2024年02月05日
    浏览(14)
  • Codeforces Round 866 (Div. 2)

    给出一个仅由_或^组成的字符串,你可以在任意位置添加_或^字符,使得字符串满足: 任意字符要么属于^_^的一部分,要么属于^^的一部分。求最少添加的字符数量。 对于_我们只需处理没有组成^_^的_: ①如果_在首位置且左边没有^则添加^ ②如果_在尾位置且右边没有^则添加

    2023年04月25日
    浏览(11)
  • Codeforces Round 867 (Div. 3)

    从所有a[i]+i-1=t的选择种取个max即可 实际上就是取同符号乘积的最大值 找规律,发现结果与边长n的关系是:res = n * (n + 3) - (n - 2) ①当n为奇数时,除了1其他均无解 ②当n为偶数时,我们可以构造一个形如n,1,n - 2,3,...的数列 首先我们可以发现n必定出现在起始位置。如果n不在起

    2024年02月02日
    浏览(12)
  • Codeforces Round 868 Div 2

    Codeforces Round 868 Div 2

    要求构造一个仅包含 (1) 和 (-1) 的长度为 (n) 的数组 (a) ,使得存在 (k) 个下标对 ((i, j), i j) 满足 (a_i times a_j = 1) 。 当有 (x) 个 (1) , (y) 个 (-1) 时,其满足条件的下标对数量为 (frac{x (x - 1)}{2} + frac{y (y - 1)}{2}) 。 由于 (n) 只有 (100) ,直接枚举 (x) 即可。

    2024年02月01日
    浏览(25)
  • Codeforces Round 926 (Div. 2)

    类似于倍投法,就是在一赔一的情况下,第一次压一块钱,每输一次就押注上一次两倍的金额. 假如资金无限的话,这种方法赢的期望为无穷大.原理类似于二进制,不论你输再多次,只要赢一次总额就增加了1.比如 15 二进制1111,前3把一直输,但是只要第4把赢,就一定可以增加 1

    2024年02月20日
    浏览(13)
  • Codeforces Round 882 (Div. 2)

    Codeforces Round 882 (Div. 2)

    目录 A. The Man who became a God  题目分析: B. Hamon Odyssey 题目分析: C. Vampiric Powers, anyone? 题目分析:  n个人分成k组,每一组的力量都是 这样的,那么如果分成k组那么就会有k-1个力量不被统计,将力量总和减去前k-1个最大的力量就是最小的结果   将数组分组,每个组内进行按位与

    2024年02月05日
    浏览(20)
  • Codeforces Round 881 (Div. 3)

    给定一个数组,给每个元素涂色。求最大的代价。 代价为每个颜色的代价和。 每个颜色的代价为涂了该颜色的元素的极差。 因为是极差,每个元素要么对答案有正的贡献,要么有负的贡献,要么无贡献。且正负贡献的个数要相同。 因为要最大值,自然就是想有正贡献的是最

    2024年02月09日
    浏览(32)
  • Codeforces Round 858 (Div. 2)题解

    目录 A. Walking Master(贪心) 题面翻译 思路: 代码实现  B. Mex Master(贪心) 题面翻译: 思路: 代码实现 C. Sequence Master(数学) 题面翻译: 思路: 代码实现 YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the

    2023年04月08日
    浏览(11)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包