"蔚来杯"2022牛客暑期多校训练营6
[题目链接]("蔚来杯"2022牛客暑期多校训练营6_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com))
G Icon Design
题目大意
给定n,按题目要求输出NFLS。
题解
见代码。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 5;
int n, cnt;
char res[maxn][maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int h = 4 * n + 5, w = 13 * n + 19;
for (int i = 1; i <= h; i++)
{
for (int j = 1; j <= w; j++)
{
if (i == 1 || j == 1 || i == h || j == w)
res[i][j] = '*';
else
res[i][j] = '.';
}
}
int x = n + 2, y = n + 3, len = 2 * n + 3;
for (int i = x; i <= x + len - 1; i++)
{
for (int j = y; j <= y + len - 1; j++)
{
if (i == j - 1 || j == y || j == y + len - 1)
res[i][j] = '@';
}
}
y += len + n + 1;
for (int i = x; i <= x + len - 1; i++)
{
for (int j = y; j <= y + len - 1; j++)
{
if (i == x || i == x + n + 1 || j == y)
res[i][j] = '@';
}
}
y += len + n + 1;
for (int i = x; i <= x + len - 1; i++)
{
for (int j = y; j <= y + len - 1; j++)
{
if (i == x + len - 1 || j == y)
res[i][j] = '@';
}
}
y += len + n + 1;
for (int i = x; i <= x + len - 1; i++)
{
for (int j = y; j <= y + len - 1; j++)
{
if (i == x || i == x + n + 1 || i == x + len - 1 || (j == y && i <= x + n + 1) || (j == y + len - 1 && i >= x + n + 1))
res[i][j] = '@';
}
}
for (int i = 1; i <= h; i++)
{
for (int j = 1; j <= w; j++)
cout << res[i][j];
cout << endl;
}
return 0;
}
J Number Game
题目大意
有四个数字A、B、C、x,有两个操作B=A−B、C=B−C,问是否可 以实现任意执行两个操作使C变为x。
题解
可以发现连续做两次 B=A−B 等于没做,连续做两次 C=B−C 等于没做,所以两个操作必然会交替的进行。
根据两个操作执行的先后顺序和次数,一共有四种结果,可以合并为两种,其中k可取任意整数:
① C=k(A-2B)+B-C;
② C=k(A-2B)+C。
注意 A=2B 时,需要特殊处理。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 5;
int t, A, B, C, x, divi;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
{
cin >> A >> B >> C >> x;
divi = A - 2 * B;
if (divi == 0)
{
if (C == x)
cout << "Yes" << endl;
else if (B - C == x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
else
{
if ((x + C - B) % divi == 0)
cout << "Yes" << endl;
else if ((x - C) % divi == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
B Eezie and Pie
题目大意
有一棵n个节点的树,根为1。每个点都有一个权值 di ,对于i点,可以从i点开始,向上跑di个点。求每个点可以遍历几次。
题解
对于点i,它最多可以向上走di,即与i深度相差为di+1及以上的点,它的子结点都少一个i。
所以可以给与i深度相差为di+1的点的子结点个数-1,这样向上递归的时候,与i深度相差为di+1及以上的点都不会统计i这个点。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
int Log2[maxn], fa[maxn][25], dep[maxn];
int n, cnt, head[maxn], res[maxn];
struct Edge
{
int to;
int next;
} edge[maxn << 1];
void init()
{
memset(head, -1, sizeof head);
cnt = 0;
}
void add(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void dfs(int cur, int fath)
{
dep[cur] = dep[fath] + 1;
fa[cur][0] = fath;
for (int i = 1; i <= Log2[dep[cur]]; ++i)
fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
for (int i = head[cur]; i != -1; i = edge[i].next)
if (edge[i].to != fath)
dfs(edge[i].to, cur);
}
int lca(int u, int depth)
{
for (int i = 20; i >= 0; i--)
if (depth <= dep[u] - (1 << i))
u = fa[u][i];
return u;
}
int solve(int u, int fath)
{
for (int i = head[u]; i != -1; i = edge[i].next)
if (edge[i].to != fath)
res[u] += solve(edge[i].to, u);
return res[u];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
for (int i = 2; i <= n; i++)
Log2[i] = Log2[i / 2] + 1;
dfs(1, 0);
for (int i = 1; i <= n; i++)
{
int d;
cin >> d;
res[i]++;
res[lca(i, max(0, dep[i] - d - 1))]--;
}
solve(1, 0);
for (int i = 1; i <= n; i++)
cout << res[i] << " ";
cout << endl;
return 0;
}
M Z-Game on grid
题目大意
两个人在 N*M 的网格上轮流移动一个棋子。棋子初始位为 (1,1) ,每次只能向某一维的正方向移动一格。网格上有一些特殊点,移到标 ’A’ 的点先手胜,移到标 ‘B’ 的点先手败,没有移到特殊点且不能再移动棋子则为平局。问先手是否有必胜、必平、必败的策略。
题解
三种胜负结果的做法是类似的,以先手胜为例。
用dp[i] [j] 表示移动到 (i,j) ,先手是否必胜。因为每次只能移一格,所以位置确定后也能知道当前是先手还是后手。
对于先手和后手,需要分开转移:
如果现在是先手动,那么只要 (i+1,j) 和 (i,j+1) 中的一个能使先手必胜,(i,j) 就是先手必胜。
如果现在是后手动,那么只有 (i+1,j) 和 (i,j+1) 都能使先手胜,(i,j) 才能使先手必胜。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e2 + 5;
int t, n, m, flag;
char a[maxn][maxn];
int dp[maxn][maxn];
bool checked(int x, int y)
{
if (x >= 1 && x <= n && y >= 1 && y <= m)
return 1;
return 0;
}
int dfs(int x, int y)
{
if (a[x][y] == 'A')
return 1;
if (a[x][y] == 'B')
return 2;
if (x == n && y == m)
return 3;
if (dp[x][y] != -1)
return dp[x][y];
if ((x + y) % 2 == 0)
{
int x1 = 0, x2 = 0;
if (checked(x + 1, y))
{
x1 = dfs(x + 1, y);
if (x1 == flag)
return dp[x][y] = x1;
}
if (checked(x, y + 1))
{
x2 = dfs(x, y + 1);
if (x2 == flag)
return dp[x][y] = x2;
}
if (x1 == 0)
return dp[x][y] = x2;
else
return dp[x][y] = x1;
}
else
{
int x1 = 0, x2 = 0;
if (checked(x + 1, y))
{
x1 = dfs(x + 1, y);
if (x1 != flag)
return dp[x][y] = x1;
}
if (checked(x, y + 1))
{
x2 = dfs(x, y + 1);
if (x2 != flag)
return dp[x][y] = x2;
}
if (x1 == 0)
return dp[x][y] = x2;
else
return dp[x][y] = x1;
}
}
int solve(int now)
{
memset(dp, -1, sizeof dp);
flag = now;
return dfs(1, 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i] + 1;
if (solve(1) == 1)
cout << "yes ";
else
cout << "no ";
if (solve(3) == 3)
cout << "yes ";
else
cout << "no ";
if (solve(2) == 2)
cout << "yes";
else
cout << "no";
cout << endl;
}
return 0;
}
A Array
题目大意
有一个长为n的数组a,且∑ 1/ai ≤1/2。现要求构造一个长为m的数组c,下标从0开始。有b数组为c数组的循环无穷数组,且必须满足在b的每个连续的ai个数中存在一个等于i的数。
题解
[大佬链接](2022牛客暑期多校训练营6 个人题解 更新至5题 - 知乎 (zhihu.com))
代码
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e7 + 5;
int t, n, m, x, c[maxn];
P v[maxn];
set<int> s;
int change(int x)
{
int ans = 1;
while (ans <= x)
ans <<= 1;
return ans >> 1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> x;
v[i] = {change(x), i};
}
sort(v + 1, v + 1 + n);
m = v[n].first;
for (int i = 1; i <= m; i++)
s.insert(i);
for (int i = 1; i <= n; i++)
{
for (int j = *s.begin(); j <= m; j += v[i].first)
{
c[j] = v[i].second;
s.erase(j);
}
}
cout << m << endl;
for (int i = 1; i <= m; i++)
{
if (c[i] == 0)
cout << 1 << " ";
else
cout << c[i] << " ";
}
cout << endl;
return 0;
}