[Kick Start] 2021 Round B
Increasing Substring
输出字符串中每个字符的最长 Increasing Substring 的长度,非常简单的动态规划问题。
定义 dp[i]
是以 str[i]
结尾的最长 Increasing Substring 的长度。
转移方程
dp[i] = dp[i-1] + 1, if str[i-1] < str[i]
dp[i] = 1, otherwise
显然是可以进行空间优化的,然而「可以但没必要」。
代码实现
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int cnt = 1;
void solve(string &str, int n)
{
vector<int> dp(n, 1);
for (int i = 1; i < n; i++)
{
if (str[i - 1] < str[i])
dp[i] = dp[i - 1] + 1;
}
printf("Case #%d:", cnt++);
for (int x : dp) printf(" %d", x);
printf("\n");
}
int main()
{
int t, n;
cin >> t;
cin.ignore();
while (t--)
{
string str;
cin >> n; cin.ignore();
cin >> str; cin.ignore();
solve(str, n);
}
}
Longest Progression
给定一个数组
令:
left[i]
表示从位置i
向左延伸,能够得到的最长等差数列的长度(包含a[i]
);right[i]
表示从位置i
向右延伸,能够得到的最长等差数列的长度(包含a[i]
)。
显然,如果我们允许改动一个位置,那么扫描数组中的任意一个数
- 组合
left[i-1]
和right[i+1]
- 条件为:
- 条件为:
- 组合
left[i-1]
和a[i], a[i+1]
- 条件为:
- 条件为:
- 组合
right[i+1]
和a[i], a[i-1]
- 条件为:
- 条件为:
- 组合
left[i-1]
和a[i]
,或者组合right[i+1]
和a[i]
,二者是必然能实现的,无需任何条件。
代码实现
#include <iostream>
#include <vector>
using namespace std;
int cnt = 1;
int solve(int n, vector<int> &a)
{
if (n <= 3) return n;
vector<int> left(n, 2), right(n, 2);
left[0] = right[n - 1] = 1;
for (int i = 2; i < n; i++)
if ((a[i] - a[i - 1]) == (a[i - 1] - a[i - 2]))
left[i] = left[i - 1] + 1;
for (int i = n - 3; i >= 0; i--)
if ((a[i + 2] - a[i + 1]) == (a[i + 1] - a[i]))
right[i] = right[i + 1] + 1;
int ans = max(left[n - 2] + 1, right[1] + 1);
for (int i = 1; i < n - 1; i++)
{
// left[i-1] + 1 其实就是组合 left[i-1] 和 a[i], 因为允许改动 a[i], 所以这是必然能实现的
// right[i+1] 与之同理
ans = max(ans, max(left[i - 1] + 1, right[i + 1] + 1));
if (i >= 2 && a[i + 1] - a[i - 1] == 2 * (a[i - 1] - a[i - 2]))
ans = max(ans, left[i - 1] + 2);
if (i + 2 < n && a[i + 1] - a[i - 1] == 2 * (a[i + 2] - a[i + 1]))
ans = max(ans, right[i + 1] + 2);
if (i >= 2 && i + 2 < n &&
a[i + 1] - a[i - 1] == 2 * (a[i - 1] - a[i - 2]) &&
a[i - 1] - a[i - 2] == a[i + 2] - a[i + 1])
ans = max(ans, left[i - 1] + right[i + 1] + 1);
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
int t, n;
cin >> t;
while (t--)
{
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) cin >> nums[i];
printf("Case #%d: %d\n", cnt++, solve(n, nums));
}
}
Consecutive Primes
给定一个整数
思路
- 令
, 求出 左侧的最大素数为 , 右侧的最小素数为 。 - 如果
,那么返回 。 - 否则,存在
, 是小于 的最大素数,返回 。
正确性证明
- 最理想的情况是
为一个整数,那么 可以得到最大乘积 。但题目要求为 2 个相邻的不同素数。 - 因此,这 2 个素数必然是下面 2 种情况之一(否则不能保证
):- 一个在
的左侧,一个在 的右侧。 - 两个都在
的左侧。
- 一个在
- 显然,如果「一左一右」的情况存在,它的乘积必然大于「均在左侧」这个乘积,因为
。
时间复杂度
- 素数判定可以在
内完成。 - 根据 Prime Gap ,两个相邻素数
之差可以记为 . - 最坏情况下,我们需要找到 3 个相邻的素数(需要扫描 2 个 Prime Gap),因此算法复杂度为
, . - 题目给定
,因此 。查表得 在 282 - 288 之间,因而这一算法复杂度是可以接受的。
代码实现
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int cnt = 1;
bool isprime(uint64_t k)
{
for (uint64_t i = 2; i * i <= k; i++)
if (k % i == 0) return false;
return true;
}
uint64_t solve(uint64_t n)
{
uint64_t k = sqrt(n);
uint64_t l = k, r = k + 1;
while (!isprime(l)) l--;
while (!isprime(r)) r++;
if (l * r <= n) return l * r;
uint64_t l2 = l - 1;
while (!isprime(l2)) l2--;
return l * l2;
}
int main()
{
int t;
cin >> t;
cin.ignore();
while (t--)
{
uint64_t n;
cin >> n;
cin.ignore();
printf("Case #%d: %llu\n", cnt++, solve(n));
}
}
Truck Delivery
给定一个树 (limit, amount)
。如果经过这一公路的卡车,它的 weight
大于等于 limit
,那么需要收费 amount
,否则不收费。
问:给定一个 weight
为
BFS/DFS
最简单,也是最暴力的解法。
思路
-
建图完成后,执行
bfs(1)
,从城市 开始 BFS,找到 到其他城市 的所有路径。路径通过一个数组pre
记录,pre[x]
表示x
的前驱城市。 -
对于每一个
,找到从 的路径,并找到所有amount
的最大公因子。 -
时间复杂度为
, 是amount
的最大值。 -
显然,这个复杂度对于 Test Case 2 来说是不可接受的。
-
第一步的 BFS 也可以换成 DFS ,因为在树中,只要遍历一次,即可找到
到 的路径。
代码实现
🥸 被数据范围搞死了,amount
的范围是 uint64_t
,我改了数据范围,但忘了改 ans
的类型;改了 ans
的类型,但忘了改 gcd
的参数;改了 gcd
的参数,但忘了改 gcd
的返回值。最后逼得我 Ctrl+F 把 int
全部替换为 uint64_t
。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
// 'first' is load-limit, 'second' is amount
typedef pair<uint64_t, uint64_t> node_t;
uint64_t cnt = 1;
unordered_map<uint64_t, unordered_map<uint64_t, node_t>> graph;
uint64_t gcd(uint64_t a, uint64_t b) { return b == 0 ? a : gcd(b, a % b); }
vector<uint64_t> bfs(uint64_t n, uint64_t start)
{
vector<uint64_t> vis(n + 1, false), pre(n + 1, 0);
queue<uint64_t> q;
vis[start] = true, q.push(start);
while (!q.empty())
{
uint64_t vex = q.front();
q.pop();
for (auto &p : graph[vex])
{
uint64_t adjacent = p.first;
if (!vis[adjacent])
vis[adjacent] = true, q.push(adjacent), pre[adjacent] = vex;
}
}
return move(pre);
}
int main()
{
ios::sync_with_stdio(0);
uint64_t t, n, q;
uint64_t x, y, limit, amount, city, weight;
cin >> t;
while (t--)
{
cin >> n >> q;
graph.clear();
for (uint64_t i = 0; i < n - 1; i++)
{
cin >> x >> y >> limit >> amount;
graph[x][y] = graph[y][x] = {limit, amount};
}
auto pre = bfs(n, 1);
cout << "Case #" << cnt++ << ": ";
while (q--)
{
cin >> city >> weight;
uint64_t cur = city, ans = 0;
while (cur != 1)
{
uint64_t prev = pre[cur];
auto [limit, amount] = graph[prev][cur];
if (weight >= limit) ans = gcd(amount, ans);
cur = prev;
}
cout << ans << " ";
}
cout << '\n';
}
}
Segment Tree
官方题解使用了线段树 (Segment Tree) ,但我不会这个数据结构 😅 , TO BE DONE.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架