AtCoder Beginner Contest 197(Sponsored by Panasonic) Person Editorial
A - Rotate
先输出第二和第三个字符,然后再输出第一个字符即可
B - Visibility
以 #
的点,注意一下会在cnt - 3
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int H, W, X, Y;
cin >> H >> W >> X >> Y;
X -= 1, Y -= 1;
vector<string> s(H);
for (int i = 0; i < H; ++i) cin >> s[i];
int cnt = 0;
// 向 4 个方向探索
for (int i = X; i < H and s[i][Y] != '#'; ++i) cnt++;
for (int i = X; i >= 0 and s[i][Y] != '#'; --i) cnt++;
for (int i = Y; i < W and s[X][i] != '#'; ++i) cnt++;
for (int i = Y; i >= 0 and s[X][i] != '#'; --i) cnt++;
cout << cnt - 3 << "\n";
return 0;
}
C - ORXOR Editorial
Good,位运算典型题
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int& x : a) cin >> x;
int ans = INT_MAX;
for (int i = 0; i < (1 << (n - 1)); ++i) {
int xored = 0, ored = 0;
for (int j = 0; j <= n; ++j) {
if (j < n) ored |= a[j];
if (j == n || (i >> j & 1)) xored ^= ored, ored = 0;
}
ans = min(ans, xored);
}
cout << ans << "\n";
return 0;
}
D - Opposite
// C++似乎内置了 PI 也可以不定义 M_PI
#define M_PI 3.14159265358979323846
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int N;
double xx, yy, x, y;
cin >> N >> xx >> yy >> x >> y;
double a = (xx + x) / 2, b = (yy + y) / 2;
xx -= a, yy -= b;
double m = xx * cos(2 * M_PI / N) - yy * sin(2 * M_PI / N);
double n = xx * sin(2 * M_PI / N) + yy * cos(2 * M_PI / N);
cout << fixed << setprecision(12) << m + a << "\n" << n + b;
return 0;
}
E - Traveler
很好的处理方法
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 7;
vector<int> v[N];
ll l[N], r[N];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
for (int i = 0, x, c; i < n; ++i) {
cin >> x >> c;
v[c].push_back(x);
}
v[0].push_back(0);
v[n + 1].push_back(0);
// int ans = 0;
for (int i = 1, j = 0; i <= n + 1; ++i) {
if (v[i].empty()) continue;
sort(v[i].begin(), v[i].end());
int lx = v[i].front(), rx = v[i].back();
int ly = v[j].front(), ry = v[j].back();
// cout << lx << " " << rx << "\n";
l[i] = min(abs(rx - ly) + l[j], abs(rx - ry) + r[j]) + rx - lx;
r[i] = min(abs(lx - ly) + l[j], abs(lx - ry) + r[j]) + rx - lx;
// ans = min(l[i], r[i]);
j = i;
}
cout << min(l[n + 1], r[n + 1]) << "\n";
return 0;
}
F - Construct a Palindrome
#include <bits/stdc++.h>
using namespace std;
const int N = 1002;
int n, m;
char s[2];
struct node {
int s, t, sp;
} r;
queue<node> q;
vector<int> a[N][26];
int ans = 1e9, vis[N][N];
//把边看成点
//对于两对边(a,b) (c,d)
//如果(a,c) 和 (b,c)之间存在边 而且边上的字母相同的话
//那么这两个边变成的点就可以联通
int bfs() {
while (!q.empty()) {
r = q.front();
q.pop();
if (r.sp == ans) return ans << 1;
for (int i = 0; i < 26; ++i)
for (int j = 0; j < a[r.s][i].size(); ++j)
for (int k = 0; k < a[r.t][i].size(); ++k) {
int ns = a[r.s][i][j];
int nt = a[r.t][i][k];
if (ns == r.t || nt == r.s) return r.sp << 1 | 1;
if (ns == nt) ans = r.sp + 1;
if (vis[ns][nt]) continue;
vis[ns][nt] = 1;
q.push((node){ns, nt, r.sp + 1});
}
}
return -1;
}
int main() {
scanf("%d%d", &n, &m);
for (int x, y; m; --m) {
scanf("%d%d%s", &x, &y, s);
a[x][*s - 'a'].push_back(y);
a[y][*s - 'a'].push_back(x);
}
vis[1][n] = 1;
q.push((node){1, n, 0});
printf("%d", bfs());
}
分类:
刷题笔记: AtCoder
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战