牛客周赛47 (已补)
比赛链接:牛客周赛47
赛时感受
又是一场思维题,应该只有EF有点算法,E需要使用快速幂和取余,F做不出,C卡了我一下,D写完了,E不写完一半又回来看C才做掉的,E也卡了很久虽然鸽巢原理想到了,但是没想到被卡在取余问题上,一开始没想出来,去做F然后做了半个小时发现做不掉,又回来在E上做功夫。
A
思路
由于需要两个元素相等,其他三个元素相等,所以直接排序,只剩下有两种情况。
代码
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 10; ll n[N]; int main() { int a, b, c, d, e; cin >> n[1] >> n[2] >> n[3] >> n[4] >> n[5]; sort(n + 1, n + 1 + 5); if ((n[3] == n[5] && n[1] == n[2]) || (n[1] == n[3] && n[4] == n[5])) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
B
思路
由于任意公共子串都能打开密码锁,所以只需要枚举26个字母是否为所有的字符串的公共子串即可。
代码
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 10; ll n; int main() { cin >> n; string s[N]; for (int i = 1; i <= n; i++) { cin >> s[i]; } for (int i = 0; i < 26; i++) { int flag = 0; for (int j = 1; j <= n; j++) { if (s[j].find('a' + i) == -1) { flag = 1; break; } } if (flag) continue; cout << (char)('a' + i); break; } return 0; }
C
思路
代码
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 10; ll n, a[N], sum, maxn, counts; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; if (a[i] > 1) counts++; maxn = max(maxn, a[i]); } if (sum % 2 == 0) { if (n == 2) { if (a[1] == a[2]) { cout << 0 << endl; } else { cout << 1 << endl; } } else if (sum <= maxn * 2) { cout << 1 << endl; } else if (maxn == 1) { cout << 0 << endl; } else { cout << counts << endl; } } else { if (sum <= maxn * 2) { cout << 1 << endl; } else { cout << n << endl; } } return 0; }
D
思路
找规律发现18个好数为一组,则下一组b数组的数组值可以由当前组a数组的数组值推理得到,即为b[i] = a[i] + 30
。
代码
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 10; ll n, a[N] = { 1, 2, 4, 5, 7, 8, 10, 11, 14, 16, 17, 19, 20, 22, 25, 26, 28, 29 }; int main() { int t; cin >> t; while (t--) { ll n; cin >> n; cout << ((n - 1) / 18) * 30 + a[((n - 1) % 18)] << endl; } return 0; }
E
思路
分类讨论横轴对称,纵轴对称和中心堆成,结果为横轴对称 + 纵轴堆成 - 中心对称
,因为中心堆成的部分都包含于横轴对称和纵轴堆成,若不减去则会计算两次。
纵轴对称:n为奇数时,纵轴所在的元素只能选择0,8,纵轴左边的元素只能选择0,8,2,5,当左边选择0,8时右边对应选择0,8,左边选择2,5时,右边对应选择5,2,所以n为奇数时纵轴对称的情况种数为:((pow(4, n / 2) * 2)
。n为偶数时,纵轴左边的元素和右边元素的选择情况和n为奇数时一致,只是不需要考虑纵轴所在的元素的选择情况,所以n为偶数时纵轴对称的情况种数为:(pow(4, n / 2) * 1)
,综合讨论得到纵轴对称的种数为:((pow(4, n / 2) * (n % 2 == 1 ? 2 : 1))
。
横轴对称:n不用考虑奇偶性,计算器上能显示的数字为1,3,8,0,所以横轴对称的情况种数为:pow(4, n)
。
中心对称:中心对称时,计算器上显示的数字只能是0,8,所以会被横轴对称和纵轴对称都计算了一遍。但是同时又需要是横轴对称又要是纵轴对称,所以中心对称的情况种数为:pow(2, ((n + 1) / 2))
。
代码
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 10; const ll mod = 1e9 + 7; ll pow(ll x, ll y) { ll res = 1; while (y) { if (y & 1) { res *= x; res %= mod; } y >>= 1; x *= x; x %= mod; } return res; } int main() { ll n; cin >> n; if (n == 1) { cout << 4 << endl; return 0; } ll res = (((pow(4ll, n / 2) * (n % 2 == 1 ? 2ll : 1ll)) % mod + pow(4ll, n)) % mod - pow(2ll, ((n + 1) / 2)) + mod); cout << (res % mod); return 0; }
F
思路
队列循环的每次找出当前节点周围四个方向上每一个为空地的格子,并更新节点数据,若更新了则加入队列再次查找,否则直接对当前列及其左右两列的所有节点都进行更新,不用特判考虑格子为障碍,直接全部当做障碍处理,因为对到达每个格子的代价都需要求最小值。
代码
#include<bits/stdc++.h> using namespace std; #define ll long long const ll N = 105, INF = 1e9; ll dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; int n, m, dis[N][N], c[N]; struct grid { int v, x, y; bool operator<(const grid k) const { if (v != k.v) return v > k.v; if (x != k.x) return x < k.x; if (y != k.y) return y < k.y; return 0; } }; char s[N][N]; priority_queue<grid>pq; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); for (int i = 1; i <= m; i++) scanf("%d", &c[i]); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) dis[i][j] = INF; dis[1][1] = 0; pq.push({0, 1, 1}); while (pq.size()) { grid u = pq.top(); pq.pop(); int ds = u.v, x = u.x, y = u.y; if (ds != dis[x][y]) continue; // 判断当前节点周围是否有没空地,并更新去到空地的代价 for (int r = 0; r < 4; r++) { int vx = x + dx[r], vy = y + dy[r]; if (vx < 1 || vx > n || vy < 1 || vy > m || s[vx][vy] == '#') continue; if (ds < dis[vx][vy]) { dis[vx][vy] = ds; pq.push({dis[vx][vy], vx, vy}); } } for (int i = 1; i <= n; i++) { for (int j = y - 1; j <= y + 1; j++) { if (j < 1 || j > m) continue; if (dis[i][j] > ds + c[j]) { dis[i][j] = ds + c[j]; pq.push({dis[i][j], i, j}); } } } } printf("%d\n", dis[n][m]); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!