#include <bits/stdc++.h>#define ll long longusing namespace std;
const int tmaxn = 2 * 1e4 + 5, zmaxn = 1e5 + 5;
int t[tmaxn], z[zmaxn];
bool f[zmaxn];
int main() {
memset(f, false, sizeof(f));
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
for (int i = 1; i <= m; i++) scanf("%d", &z[i]);
sort(t + 1, t + 1 + n);
sort(z + 1, z + 1 + m);
ll sum = 0;
for (int i = 1; i <= n; i++) {
int flag = false;
for (int j = 1; j <= m; j++) if (f[j]) continue;
else if (z[j] >= t[i] && !f[j]) {
sum += z[j];
f[j] = flag = true;
break;
}
if (!flag) {
printf("you died!");
return 0;
}
}
printf("%lld", sum);
return 0;
}
#include <bits/stdc++.h>using namespace std;
const int maxn = 1e5 + 5;
int a[maxn], r[maxn], g[maxn];
int main() {
int n;
scanf("%d %d", &n, &a[1]);
for (int i = 2; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &r[i]);
for (int i = 1; i <= n; i++) scanf("%d", &g[i]);
int res = 0;
for (int i = 1; i <= n; i++) {
res += a[i];
int x = res % (r[i] + g[i]);
if (x > g[i]) res += r[i] - (x - g[i]);
printf("%d\n", res);
}
return 0;
}
T7 P1815 蠕虫游戏
蛇在(25,11) ~ (25, 30)这一块
也就是蛇占了第25行中的11列到30列
然后按照题目给定的指令进行移动
每走一步,就判断有没有撞到东西,如果撞到了就直接结束
如果在第5步撞到了自己,那么直接结束并且输出
The worm ran into itself on move 5.
如果在第9步撞到了墙,那么直接结束并且输出
The worm ran off the board on move 9.
如果我们将所有的20条指令都完成了,没有撞到任何的东西,那么输出
The worm successfully made all 20 moves.
代码
C++
#include <bits/stdc++.h>using namespace std;
struct node {
int x, y;
} a[30];
int main() {
int n;
string s;
while (scanf("%d", &n)) {
if (n == 0) break;
cin >> s;
for (int i = 1; i <= 20; i++)
a[i].x = 25, a[i].y = 10 + i;
int headX = 25, headY = 30;
int succeed = 0;
int m = 0;
for (int i = 0; i < n; i++) {
m++;
if (s[i] == 'E') headY++;
else if (s[i] == 'W') headY--;
else if (s[i] == 'S') headX++;
else headX--;
if (headX > 50 || headX < 1 || headY > 50 || headY < 1) {
succeed = 2;
break;
}
for (int j = 1; j < 20; j++) a[j] = a[j + 1];
a[20].x = headX, a[20].y = headY;
for (int j = 1; j < 20; j++) if (headX == a[j].x && headY == a[j].y) {
succeed = 1;
break;
}
if (succeed != 0) break;
}
if (succeed == 0) printf("The worm successfully made all %d moves. \n", m);
else if (succeed == 1) printf("The worm ran into itself on move %d.\n", m);
else printf("The worm ran off the board on move %d. \n", m);
}
return 0;
}