马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
目次
A. 末了DISCO
D. 太阳系DISCO
E. 平凡DISCO-1
A. 末了DISCO
(1)pow(a, b) :a ^ b
(2)数据范围,爆long long。因此直接判 a ^ b 的奇偶,奇赋值 1 ,偶赋值 0
(3)注意 b 大概为 0
- #include<bits/stdc++.h>
- #define LL long long
- using namespace std;
- LL a, b, c, d, n, ans;
- int main()
- {
- cin >> a >> b >> c >> d;
- LL t1 = 2;
- if (a % 2 != 0 || b == 0)
- t1 = 1;
- ans = t1 * c + d;
- if (ans % 2 == 0)
- cout << "NO";
- else
- cout << "YES";
- return 0;
- }
复制代码
D. 太阳系DISCO
(1)标题实质为最短路,每次耗费均为 1,用 bfs 即可
(2)逆时针走 y 步,即为顺时针走 n - y 步,题中规定 y < n,否则模前要 + n
(3)每一个队首出队都有顺 / 逆两种选择,到达的两个点都入队
(4)技能最多用一次,用两次便是没用。和不消技能作比力
(5)技能在哪用都一样,那就选择体如今末了一次上
- #include<bits/stdc++.h>
- #define LL long long
- using namespace std;
- const int N = 2e5 + 5, INF = 0x3f3f3f3f;
- int n, k, a, b, x, y, ans, dis[N];
- queue <int> q;
- int main()
- {
- cin >> n >> k >> a >> b >> x >> y;
- memset(dis, 0x3f, sizeof(dis));
- q.push(a), dis[a] = 0;
- while (q.size())
- {
- int p = q.front();
- q.pop();
- int e1 = (p + x) % n;
- if (dis[e1] > dis[p] + 1)
- {
- dis[e1] = dis[p] + 1;
- q.push(e1);
- }
- int e2 = (p + n - y) % n;
- if (dis[e2] > dis[p] + 1)
- {
- dis[e2] = dis[p] + 1;
- q.push(e2);
- }
- }
- if (dis[b] == INF)
- ans = -1;
- else
- ans = dis[b];
- if (k)
- ans = min(ans, dis[(b + n / 2) % n] + 1);
- cout << ans;
- return 0;
- }
复制代码
E. 平凡DISCO-1
(1)找树最深的一条链,把其叶子结点作为被换对象
(2)该条链上每一个点作为父节点,撤消链上的下一个子节点外,其他的子节点作为根节点是的最大深度,找到最大的值,作为被换对象
(3)son[] 数组里存的是最长链各节点值
- #include<bits/stdc++.h>
- #define LL long long
- using namespace std;
- const int N = 5e5 + 5, INF = 0x3f3f3f3f;
- int n, ans, dep[N], son[N];
- vector<int> G[N];
- void dfs(int u, int fa)
- {
- dep[u] = 1;
- for (int i = 0; i < G[u].size(); i ++)
- {
- int v = G[u][i];
- if (v != fa)
- {
- dfs(v, u);
- dep[u] = max(dep[u], dep[v] + 1);
- if (dep[v] > dep[son[u]])
- son[u] = v;
- }
- }
- }
- void add(int u, int fa)
- {
- for (int i = 0; i < G[u].size(); i ++)
- {
- int v = G[u][i];
- if (v != fa && v != son[u])
- ans = max(ans, dep[v]);
- }
- if (son[u]) // 还没到最长链的叶子结点
- add(son[u], u);
- }
- int main()
- {
- cin >> n;
- for (int i = 1; i < n; i ++)
- {
- int u, v;
- cin >> u >> v;
- G[u].push_back(v);
- G[v].push_back(u);
- }
- dfs(1, 0);
- add(1, 0);
- cout << dep[1] + max(ans - 1, 0);
- return 0;
- }
复制代码 |