A.Shortest Path with Obstacle
思路
起点、终点和障碍在同一直线,且障碍在起点终点之间则要多走 2 2 2 步绕开障碍
Accepted code
#include <bits/stdc++.h>
using namespace std;
int t;
int sx, sy, fx, fy, tx, ty;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &sx, &sy);
scanf("%d%d", &tx, &ty);
scanf("%d%d", &fx, &fy);
int ans = abs(sx - tx) + abs(sy - ty);
if (sx == fx && sx == tx && fy > min(sy, ty) && fy < max(sy, ty))
ans += 2;
if (sy == fy && sy == ty && fx < max(sx, tx) && fx > min(sx, tx))
ans += 2;
printf("%d\n", ans);
}
return 0;
}
B.Alphabetical Strings
思路
我是从 a
的两侧开始找,用两个指针找下一个字母,注意指针越界的情况。
Accepted code
#include <bits/stdc++.h>
using namespace std;
int t;
int l, r;
int ok, flag, idx;
char now;
char s[35];
int main() {
scanf("%d", &t);
while (t--) {
ok = 0;
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] == 'a') {
ok = 1;
flag = i;
}
}
now = 'a';
idx = 0;
if (ok) {
l = flag - 1;
r = flag + 1;
while (1) {
if (idx >= len - 1) {
break;
}
if (l >= 0 && s[l] == (char)(now + 1)) {
--l;
++now;
++idx;
} else if (r < len && s[r] == (char)(now + 1)) {
++r;
++now;
++idx;
} else {
ok = 0;
break;
}
}
if (ok)
puts("YES");
else
puts("NO");
} else {
puts("NO");
}
}
return 0;
}
C. Pair Programming
思路
数组 a , b a,b a,b 各一个指针,优先选择数值小的,注意指针越界的情况。
Accepted code
#include <bits/stdc++.h>
using namespace std;
int t;
int k, n, m;
int a[105];
int b[105];
int ans[205];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &k, &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d", &b[i]);
}
int aidx = 0, bidx = 0, idx = 0;
int ok = 1;
while (1) {
if (idx >= n + m) break;
if (aidx < n && (bidx >= m || a[aidx] < b[bidx])) {
if (a[aidx] == 0)
++k;
else if (a[aidx] > k) {
ok = 0;
break;
}
ans[idx] = a[aidx];
++aidx;
} else if (bidx < m) {
if (b[bidx] == 0)
++k;
else if (b[bidx] > k) {
ok = 0;
break;
}
ans[idx] = b[bidx];
++bidx;
} else {
ok = 0;
break;
}
++idx;
}
if (ok) {
for (int i = 0; i < n + m; i++) {
printf("%d%c", ans[i], i == n + m - 1 ? '\n' : ' ');
}
} else {
puts("-1");
}
}
return 0;
}
D. Co-growing Sequence
思路
对于每个数,都将其与前一个数相或求出当前数应当化为的数,再将求出的数与当前数异或,所得的数即为 y i y_i yi 。
Accepted code
#include <bits/stdc++.h>
using namespace std;
int t;
int n;
int a[200005];
int ans[200005];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (i) {
int to = (a[i] | a[i - 1]);
ans[i] = a[i] ^ to;
a[i] = to;
}
}
for (int i = 0; i < n; i++) {
printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');
}
}
return 0;
}