2020ICPC·小米 网络选拔赛第一场
2020ICPC·小米 网络选拔赛第一场
C-Smart Browser
#include <string>
#include <iostream>
std::string s;
void solve() {
std::cin >> s;
int ans = 0, t = s[0] == 'w';
for (int i = 1; i < s.size(); i++) {
if (s[i] == 'w') {
t++;
} else {
if (t != 0) {
ans += 2 * t - 1;
t = 0;
}
}
}
if (t != 0) {
ans += 2 * t - 1;
}
printf("%d\n" ,ans);
}
int main() {
solve();
return 0;
}
I-Walking Machine
思路:
一道模拟+DFS。对于每个点检查它的四个方向上的点能不能到达这个点,可以的话就DFS。
代码中处理了大量重复片段,一来减小了代码量,二来方便查错,具体细节在代码中体现。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc sesond
const int Maxn = 1005;
const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // W S A D
int a[Maxn][Maxn], n, m, ans = 0;
bool vis[Maxn][Maxn];
inline int read() {
char ch, tar[] = "WSAD";
while ((ch = getchar()) != EOF) {
for (int i = 0; i < 4; i++) {
if (ch == tar[i]) {
return i;
}
}
}
return -1;
}
std::pii getDest(int x, int y) {
int d = a[x][y];
int xx = x + dir[d][0];
int yy = y + dir[d][1];
return std::mp(xx, yy);
}
void run(int x, int y, int xx, int yy);
void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx >= 1 && yy >= 1 && xx <= n && yy <= m) {
run(x, y, xx, yy);
}
}
}
void run(int x, int y, int xx, int yy) {
if (std::mp(x, y) == getDest(xx, yy) && !vis[xx][yy]) {
vis[xx][yy] = true;
ans++;
dfs(xx, yy);
}
}
void solve() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = read();
}
}
for (int i = 1; i <= n; i++) {
run(i, 0, i, 1);
run(i, m + 1, i, m);
}
for (int i = 1; i <= m; i++) {
run(0, i, 1, i);
run(n + 1, i, n, i);
}
printf("%d\n", ans);
}
int main() {
solve();
return 0;
}
A-Intelligent Warehouse
思路:
记忆化搜索。对于每个存在的数字进行一次搜索,而一些数字可能在搜索之前的数字时就被搜索过,那么直接返回之前搜索过的结果即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
const int Maxn = 10000005;
int in[Maxn], a[Maxn], cnt[Maxn], maxx;
int dfs(int cur) {
if (cnt[cur] != 0) {
return cnt[cur];
}
int res = a[cur]; // 至少应该包括他自己
for (int i = 2; i * cur <= maxx; i++) {
if (a[i * cur]) {
res = std::max(res, a[cur] + dfs(i * cur));
}
}
cnt[cur] = res;
return res;
}
void solve() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", in + i);
maxx = std::max(maxx, in[i]);
a[in[i]]++;
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = std::max(ans, dfs(in[i]));
}
printf("%d\n", ans);
}
int main() {
solve();
return 0;
}