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 与边数
代码
#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]