B、牛牛战队的比赛地
其实应该算是一个三分的板子题吧。
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 #define PI acos(-1) const double eps = 1e-9; const int maxn = 1e5 + 10; using namespace std; struct point { int x,y; }p[maxn]; int n; double f(double x) { double max = 0; for (int i = 1;i <= n;i++) { double temp = sqrt(p[i].y*p[i].y+(p[i].x-x)*(p[i].x-x)); if (temp > max) max = temp; } return max; } int main() { scanf("%d",&n); for (int i = 1;i <= n;i++) scanf("%d%d",&p[i].x,&p[i].y); double l = -10000,r = 10000; while (l + eps < r) { double m1 = l + (r-l) / 3; double m2 = r - (r-l) / 3; if (f(m1) > f(m2)) l = m1; else r = m2; } printf("%.4lf\n",f(l)); return 0; }
D、牛牛与牛妹的约会
首先要注意 STL 里面的 pow(a,b) 函数无法处理 a为负数的情况。
贪心。比较直接走和闪现哪一个可以走的更快,选择更快的方法去走即可。
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 #define PI acos(-1) const double eps = 1e-9; const int maxn = 1e5 + 10; using namespace std; int main() { int T; scanf("%d",&T); while (T--) { double a,b; scanf("%lf%lf",&a,&b); double ans = 0.0; double p = 1.0 / 3.0; while (1) { double temp; if (a < 0) temp = -pow(-a,p); else temp = pow(a,p); if (abs(temp-b)+1.0 < abs(a-b)) { ans += 1.0; a = temp; } else { ans += abs(a-b); break; } } printf("%.9lf\n",ans); } return 0; }
F、碎碎念
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 #define PI acos(-1) const double eps = 1e-9; const int maxn = 1e5 + 10; const LL mod = 1e9 + 7; using namespace std; LL dp[maxn][3]; int main() { int x,q; cin >> x >> q; dp[0][0] = 1; for (int i = 1;i < maxn;i++) { dp[i][0] = (dp[i-1][0] + dp[i-1][1])%mod; if (i >= x ) dp[i][1] = dp[i-x][0] % mod; } dp[1][2] = dp[1][0] + dp[1][1]; for (int i = 2;i < maxn;i++) { dp[i][2] = (dp[i-1][2] + dp[i][0] + dp[i][1] ) % mod; } while (q--) { int l,r; cin >> l >> r; LL ans = dp[r][2] - dp[l-1][2]; cout << (ans + mod) % mod << endl; } return 0; }
G、街机争霸
首先先看僵尸的移动,很容易发现僵尸移动的周期是 2*(k-1) ,所以我们需要开一个三维数组去记录这 2*(k-1) 时间里面僵尸的位置
其他的话就和普通的 bfs 没什么区别了
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 #define PI acos(-1) const double eps = 1e-9; const int maxn = 600; const LL mod = 1e9 + 7; using namespace std; int n,m,p,k; int vis[maxn][maxn]; int T[maxn][maxn][20]; char s[maxn][maxn]; int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}; int ex,ey,sx,sy; struct node { int x,y; int cnt; }; bool check(int x,int y,int tim) { if (vis[x][y] || x <= 0 || x > n || y <= 0 || y > m || s[x][y] == '&') return true; if (T[x][y][tim % (2 * k - 2)]) return true; return false; } int bfs(int x,int y) { queue<node> q; q.push({x,y,0}); vis[x][y] = 1; while (!q.empty()) { node now = q.front(); q.pop(); if (now.x == ex && now.y == ey) return now.cnt; for (int i = 0;i < 4;i++) { int xx = now.x + dir[i][0]; int yy = now.y + dir[i][1]; if (check(xx,yy,now.cnt+1)) continue; vis[xx][yy] = 1; q.push({xx,yy,now.cnt+1}); } } return -1; } int main() { cin >> n >> m >> p >> k; getchar(); for (int i = 1;i <= n;i++) { scanf("%s",s[i]+1); for (int j = 1;j <= m;j++) { if (s[i][j] == 'L') { sx = i; sy = j; } if (s[i][j] == 'A') { ex = i; ey = j; } } } for (int i = 1;i <= p;i++) { char str[10]; int x,y; cin >> x >> y; cin >> str; T[x][y][0] = T[x][y][2*k-2] = 1; if (str[0] == 'R') { for (int j = 1;j < k;j++) { x = x + dir[0][0]; y = y + dir[0][1]; T[x][y][j] = T[x][y][2*k-2-j] = 1; } } if (str[0] == 'L') { for (int j = 1;j < k;j++) { x = x + dir[1][0]; y = y + dir[1][1]; T[x][y][j] = T[x][y][2*k-2-j] = 1; } } if (str[0] == 'U') { for (int j = 1;j < k;j++) { x = x + dir[2][0]; y = y + dir[2][1]; T[x][y][j] = T[x][y][2*k-2-j] = 1; } } if (str[0] == 'D') { for (int j = 1;j < k;j++) { x = x + dir[3][0]; y = y + dir[3][1]; T[x][y][j] = T[x][y][2*k-2-j] = 1; } } } int ans = bfs(sx,sy); if (ans == -1) cout << "Oh no" << endl; else cout << ans << endl; }
H、hash
观察给的哈希函数可以轻松看出,这个哈希函数就仅仅是把一个只由小写字符组成的字符串当成了一个26进制的数,不取模的情况下,任意小写字符串和自然数是一一对应的。因此,只要把给的字符串转换成对应的26进制整数,加上模数后转换回字符串,一定可以得到一个最小字典序大于原串的字符串。只要这个新字符串长度为6即是满足要求的字符串。
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 #define PI acos(-1) const double eps = 1e-8; const int maxn = 1e5 + 10; using namespace std; char str[15]; int mod; int main() { while (~scanf("%s%d",str,&mod)) { LL res = 0; for (int i = 0;i < 6;i++) res = res * 26 + str[i] - 'a'; res += mod; for (int i = 5;i >= 0;i--) { str[i] = res % 26 + 'a'; res /= 26; } if (res) printf("-1\n"); else { printf("%s\n",str); } } return 0; }