【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)

本文提供了 Codeforces 791 题目的解题思路与代码实现,涵盖了模拟、图论、贪心算法及树形DP等算法技巧。

题目链接


A. Bear and Big Brother(Codeforces 791A)

思路

由于 220 320 都会超过 106 ,所以只要模拟 20 年两人的体重变化就可以了。当然也可以列方程解之,不过太麻烦。

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll a, b, x, y;

int main() {
//    freopen("Input3.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> a >> b;
    x = 1;
    y = 1;
    for(int i = 1; i <= 20; i++) {
        x *= 3;
        y *= 2;
        if(a * x > b * y) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

B. Bear and Friendship Condition(Codeforces 791B)

思路

这种有“关系”的题目,用图来建模最合适不过。所以问题转化为每个连通分量的子图是否构成“完全图”,也就是两点之间是否都有边相连。完全图有一个性质:其点数 n 与边数 m 满足 m=n×(n1)2 。利用这个性质我们可以用 DFS 的方式(或者 BFS )访问每个连通分量,在每个分量内统计点数和边数。就可以判断该图是否合理了。在算乘法的时候注意防溢出。

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 2e5;
vector <int> G[maxn];
bool vis[maxn];
int u, v, n, m;
ll V, E;

void dfs(int u) {
    vis[u] = true;
    E += G[u].size();
    V++;
    for(int v : G[u]) {
        if(vis[v] == true) {
            continue;
        }
        dfs(v);
    }
}

int main() {
//    freopen("Input1.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    while(m--) {
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++) {
        if(vis[i] == true) {
            continue;
        }
        V = E = 0;
        dfs(i);
        if(V * (V - 1) != E) {
            puts("NO");
            return 0;
        }
    }
    puts("YES");
    return 0;
}

C. Bear and Different Names(Codeforces 791C)

思路

本题只需要构造一组解。搜索的话复杂度太高,可以先考虑贪心的思想。首先应当明确,输入中的一个字符串 YES NO 描述的是一个窗口的情况,也就是窗口中的名字是否有重复。
例如 k=4 的某个窗口 [1,1,2,3,4] 是有重复的, k=5 的某个窗口 [3,4,1,5,2] 是没有重复的。注意,这里名字的描述方式是数字,答案要求输出真实人名,我们只需要将数字映射到人名上就行了
如果窗口的描

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值