牛客周赛 Round 8
牛客周赛 Round 8
A-小美的排列询问_牛客周赛 Round 8 (nowcoder.com)
枚举即可
#include<bits/stdc++.h> using i64 = long long; using namespace std; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> a(N); for(auto &i : a) cin >>i; int x,y; cin >> x >> y; for(int i = 1;i < N - 1;i ++){ if(a[i] == x){ if(a[i - 1] == y || a[i + 1] == y){ cout << "Yes\n"; return 0; } }else if(a[i] == y){ if(a[i - 1] == x || a[i + 1] == x){ cout << "Yes\n"; return 0; } } } cout << "No\n"; return 0; }
B-小美走公路_牛客周赛 Round 8 (nowcoder.com)
因为公路是环形的,所以我们可以把公路往后再展开\(N\)个站,然后用一个前缀和和计算两两车站之间的距离,如果是要往前走,那就是用\(sum[y] - sum[x]\),往后走就是\(sum[x+N] - sum[y]\),就可以想象成从\(y\)这个点走过一个环形到达\(x\)站,因为这里都是往前计算的前缀和,所以计算的时候我们要把大的站放前面,小的站放后面
#include<bits/stdc++.h> using i64 = long long; using namespace std; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i ++) cin >> a[i]; for (int i = 0; i < N; i ++) { a.push_back(a[i]); } int x, y; cin >> x >> y; x--, y--; if(x > y) swap(y,x); vector<i64> sum(a.size()); for (int i = 1; i < a.size(); i++) sum[i] = sum[i - 1] + a[i - 1]; cout << min(sum[y] - sum[x], sum[x + N] - sum[y]) << '\n'; return 0; }
C-小美的排列构造_牛客周赛 Round 8 (nowcoder.com)
我是直接造了几组数据然后猜了个结论,比如当\(n=6\)时,可以构造一个\(1,6,2,5,3,4\)这样就可以得到\(7,8,7,8,7\),又比如当\(n=5\)时,可以构造\(1,5,2,4,3\),可以得到\(6,7,6,7\),可以肯定的是权值最小都可以得到\(1\),再进一步观察可以发现其实就是将\(1 \sim n\)分成两半,然后后面大的数倒序插入前面
#include<bits/stdc++.h> using i64 = long long; using namespace std; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int m = n / 2; vector<int> b; while(m){ b.push_back(n); n--; m--; } for(int i = 1;i <= n;i ++){ cout << i << ' '; cout << b[i - 1] << ' '; } return 0; }
D-小美的树上染色_牛客周赛 Round 8 (nowcoder.com)
\(dfs\)每次去搜节点的子孙是否能优先被染红
#include<bits/stdc++.h> using i64 = long long; using namespace std; typedef pair<i64, i64> PII; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n ; vector<int> a(n); for (auto &i : a) cin >> i; vector<vector<int>> g(n); for (int i = 1, x, y; i < n; i ++) { cin >> x >> y; x--, y--; g[x].push_back(y); g[y].push_back(x); } int ans = 0; vector<bool> v(n, false); auto dfs = [&](auto self, int x, int u) -> void{ for (auto i : g[x]) { if (i == u) continue; self(self, i, x); int res = a[i] * a[x]; int d = sqrt(res); if (!v[i] && !v[x] && d * d == res) { v[i] = v[x] = true; ans += 2; } } }; dfs(dfs, 0, -1); cout << ans << '\n'; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~