https://ac.nowcoder.com/acm/contest/322#question
A.dreamstart的催促
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const long long mod = 10000019LL; long long Pow(long long a, long long b) { long long ans1 = 1; a = a % mod; while(b) { if(b % 2) { ans1 = (ans1 * a) % mod; b --; } else { a = (a * a) % mod; b /= 2; } } return ans1; } int main() { int n; scanf("%d", &n); long long ans = 0; for(int i = 1; i <= n; i ++) { long long x; scanf("%lld", &x); ans = (ans + Pow(x, 1LL * i)) % mod; } cout << ans << endl; return 0; }
B.TRDD got lost again
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <queue> using namespace std; const int maxn = 6010; int n, m; char s[maxn][maxn]; int f[maxn][maxn]; int sx, sy; int ex, ey; int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; int out(int x, int y) { if(x < 0 || x > n) return 1; if(y < 0 || y > m) return 1; return 0; } int main() { scanf("%d%d", &n, &m); getchar(); n = 2 * n + 1; m = 2 * m + 1; for(int i = 0; i < n; i ++) { gets(s[i]); for(int j = 0; s[i][j]; j ++) { if(s[i][j] == 'S') s[i][j] = ' ', sx = i, sy = j; if(s[i][j] == 'T') s[i][j] = ' ', ex = i, ey = j; } } f[sx][sy] = 1; queue<int> q; q.push(sx * m + sy); while(!q.empty()) { int pr = q.front(); q.pop(); int nx = pr / m; int ny = pr % m; for(int i = 0; i < 4; i ++) { int tx = nx + dir[i][0]; int ty = ny + dir[i][1]; if(out(tx, ty)) continue; if(f[tx][ty]) continue; if(s[tx][ty] != ' ') continue; f[tx][ty] = f[nx][ny] + 1; q.push(tx * m + ty); } if(f[ex][ey]) break; } if(f[ex][ey] == 0) { printf("TRDD Got lost...TAT\n"); } else { printf("%d\n", (f[ex][ey] + 1) / 2); } return 0; }
C.Company
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <queue> using namespace std; const int maxn = 2e5 + 10; int n, k; int a[maxn]; int v[maxn * 2], nx[maxn * 2], h[maxn], sz; int ans[maxn], f[maxn]; void add(int U, int V) { v[sz] = V; nx[sz] = h[U]; h[U] = sz ++; } void dfs(int x) { f[x] = 1; for(int i = h[x]; i != -1; i = nx[i]) { if(f[v[i]]) continue; dfs(v[i]); ans[x] += ans[v[i]]; } ans[x] += a[x]; } int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i ++) { int x; scanf("%d", &x); a[i] = (x <= k); } for(int i = 0; i <= n; i ++) h[i] = -1; int sz = 0; for(int i = 0; i < n - 1; i ++) { int U, V; scanf("%d%d", &U, &V); add(U, V); add(V, U); } dfs(1); for(int i = 1; i <= n; i ++) { printf("%d", ans[i]); if(i < n) printf(" "); else printf("\n"); } return 0; }
D.>A->B->C-
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 5e3 + 10; int n; int x[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; i ++) { scanf("%d", &x[i]); } int ans = 0; for(int i = 1; i <= n; i ++) { int a = i; int b = x[i]; int c = x[b]; if(a == x[c]) { ans = 1; break; } } if(ans) printf("YES\n"); else printf("NO\n"); return 0; }
E.PPY的字符串
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <queue> using namespace std; const int maxn = 1e5 + 10; int x, n; int a[maxn]; vector<int> F(vector<int> in) { vector<int> ans; queue<int> q; int L = 0, R = 0; while(L < in.size()) { if(R < in.size() && in[R] == in[L]) R ++; else { // [L, R - 1] are same q.push(R - L); q.push(in[L]); L = R; } } while(!q.empty()) { int num = q.front(); q.pop(); if(num == 0) { ans.push_back(0); continue; } stack<int> st; while(num) { st.push(num % 10); num = num / 10; } while(!st.empty()) { ans.push_back(st.top()); st.pop(); } } return ans; } int main() { scanf("%d%d", &x, &n); stack<int> st; while(x) { st.push(x % 10); x = x / 10; } vector<int> in; while(!st.empty()) { in.push_back(st.top()); st.pop(); } n --; while(n --) { in = F(in); } cout << in.size() << " "; for(int i = 0; i < in.size(); i ++) { cout << in[i]; } cout << endl; return 0; }
F.集训队脱单大法:这是一道只能由学姐我自己出数据的水题
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1e5 + 10; int n; int a[maxn], p[maxn], q[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; i ++) { scanf("%d", &a[i]); } p[1] = a[1]; for(int i = 2; i <= n; i ++) p[i] = max(a[i], p[i - 1]); q[n] = a[n]; for(int i = n - 1; i >= 1; i --) q[i] = max(a[i], q[i + 1]); int ans = 0; for(int i = 1; i <= n - 1; i ++) { ans = max(ans, abs(p[i] - q[i + 1])); } cout << ans << endl; return 0; }
G.不想再WA了
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int T, n; int dp[20][5]; int main() { scanf("%d", &T); while(T --) { scanf("%d", &n); dp[1][1] = 1; dp[1][2] = 1; dp[1][3] = 1; for(int i = 2; i <= n; i ++) { dp[i][1] = dp[i - 1][1] + dp[i - 1][2]; dp[i][2] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3]; dp[i][3] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3]; } cout << dp[n][1] + dp[n][2] + dp[n][3] << endl; } return 0; }
H.Ricky’s RealDan’s Ricky
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <queue> using namespace std; int main() { int T; scanf("%d", &T); while(T --) { int n; scanf("%d", &n); int a[2] = {0, 0}; for(int i = 1; i <= n; i ++) { int x; scanf("%d", &x); a[x % 2] ++; } if(n == 1 && a[0] == 1) printf("Ricky is Winner\n"); else printf("RealDan is Winner\n"); } return 0; }
I.小A的期末作业
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int n; int main() { scanf("%d", &n); int space = 0; for(int i = 1; i <= n; i ++) { for(int j = 0; j < space; j ++) printf(" "); for(int j = 1; j <= n; j ++) printf("*"); printf("\n"); space ++; } space --; for(int i = n + 1; i <= 2 * n - 1; i ++) { space --; for(int j = 0; j < space; j ++) printf(" "); for(int j = 1; j <= n; j ++) printf("*"); printf("\n"); } return 0; }
J.怪盗基德 & 月之瞳宝石
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 10; int n, m; long long a[maxn], b[maxn]; int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i ++) scanf("%lld", &a[i]); for(int i = 1; i <= m; i ++) scanf("%lld", &b[i]); sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m); long long ans = 0; for(int i = 1; i <= n; i ++) { int L, R, pos; long long dis = 1e18; L = 1, R = m, pos = -1; while(L <= R) { int mid = (L + R) / 2; if(b[mid] <= a[i]) pos = mid, L = mid + 1; else R = mid - 1; } if(pos != -1) dis = min(dis, abs(a[i] - b[pos])); L = 1, R = m, pos = -1; while(L <= R) { int mid = (L + R) / 2; if(b[mid] >= a[i]) pos = mid, R = mid - 1; else L = mid + 1; } if(pos != -1) dis = min(dis, abs(a[i] - b[pos])); ans = max(ans, dis); } cout << ans << endl; return 0; }
K.正方体
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int T; int p, q; int a[5][5]; int main() { scanf("%d", &T); for(int cas = 0; cas < T; cas ++){ for(int i = 0; i < 3; i ++) { for(int j = 0; j < 4; j ++) { scanf("%d", &a[i][j]); if(i == 0 && a[i][j]) p = a[i][j]; if(i == 2 && a[i][j]) q = a[i][j]; } } int ans = 1; if(p != q) ans = 0; if(a[1][0] != a[1][2]) ans = 0; if(a[1][1] != a[1][3]) ans = 0; if(ans) printf("Yes!\n"); else printf("No!\n"); if(cas % 50 == 49) printf("\n"); } return 0; }
L.简单的分数
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; struct X { int a, b; X(int aa, int bb) { a = aa; b = bb; } }; int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a % b); } X add(X x, X y) { X ans(0, 0); /* x.a y.a x.a * y.b + y.a * x.b ----- + ----- = ----------------------- x.b y.b x.b * y.b */ ans.a = x.a * y.b + y.a * x.b; ans.b = x.b * y.b; if(ans.a == 0) { return X(0, 1); } if(ans.b < 0) { ans.a = -ans.a; ans.b = -ans.b; } int g = gcd(abs(ans.a), abs(ans.b)); ans.a /= g; ans.b /= g; return ans; } int main() { int T; scanf("%d", &T); while(T --) { int op; int a, b, c, d; scanf("%d", &op); scanf("%d%d%d%d", &a, &b, &c, &d); if(op == 0) c = -c; X p1(a, b); X p2(c, d); X ans = add(p1, p2); printf("%d/%d\n", ans.a, ans.b); } return 0; }
M.HJ浇花
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1e6 + 10; int n; int a[maxn]; int b[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; i ++) { int L, R; scanf("%d%d", &L, &R); a[L] ++; a[R + 1] --; } for(int i = 0; i < maxn; i ++) { if(i > 0) a[i] = a[i] + a[i - 1]; b[a[i]] ++; } for(int i = 1; i <= n; i ++) { printf("%d", b[i]); if(i < n) printf(" "); else printf("\n"); } return 0; }
这套题目是 1.1 和 FH 一起刷的 新年第一套题 写每一道题的过程都是开心的 新的一年少写 bug 哦!