StarryCoding 入门教育赛 3 题解C++

2024-05-11 06:12

本文主要是介绍StarryCoding 入门教育赛 3 题解C++,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

比赛链接:https://www.starrycoding.com/contest/8

A. 坐标变换

语法题,考察输入输出、循环结构、数学运算。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;const ll p = 998244353;
const int N = 2e5 + 9;void solve()
{int n;cin >> n;ll x = 0, y = 0;for (int i = 1; i <= n; ++i){ll dx, dy;cin >> dx >> dy;x += dx, y += dy;}cout << x << ' ' << y << '\n';
}int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int _;cin >> _;while (_--)solve();return 0;
}

B. 自助店评分

考察STL中struing, map的使用。

对STL不熟悉的同学可以看下《算法基础课》:https://www.starrycoding.com/course/1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;const ll p = 998244353;
const int N = 2e5 + 9;void solve()
{map<string, ll> mp;int n, m;cin >> n >> m;for (int i = 1; i <= n; ++i){string s;ll x;cin >> s >> x;mp[s] = x;}ll ans = 0;for (int i = 1; i <= m; ++i){string s;cin >> s;ans = max(ans, mp[s]);}cout << ans << '\n';
}int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int _;cin >> _;while (_--)solve();return 0;
}

C.打字糕手

考察题目理解能力,字符串和下标变换灵活运用。

#include <bits/stdc++.h>
using namespace std;void solve()
{string s, t;cin >> s >> t;for(int i = 0, j = 0;i < t.length() && j < s.length(); ++ i){if(t[i] == s[j]){cout << i + 1 << ' ';j ++;}}cout << '\n';
}int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int _;cin >> _;while(_ --)solve();return 0;
}

D.电弧陷阱

类似这种给一个矩阵,然后有些位置不能去、被挡住、有传送门之类的,一般都需要用到 b f s bfs bfs。这种题就类似于走迷宫,需要保证走过的点不要再走,否则会让时间复杂度变高。

显然电弧陷阱会将整个矩阵分成好几部分连通区域,我们只需要在遍历矩阵的时候判断一下当前位置,是否可以作为出发点即可开始 b f s bfs bfs

此时一个连通部分的答案 = . 的数量 + 电弧数量。

. 的数量我们可以通过找到所有不会被电弧困住的地方来计算,而难点在于如何算出 电弧 的数量。

最简单的方法就是使用set维护一个pair<x, y>直接去重,最后加上 set.size() 即可。

容易出错的地方在于,如果选择将 # 周围的点修改为其他字符做标记,那么一定要在 输入完成后进行bfs之前 进行标记。

还需要注意,题目保证了至少一个点为 . ,因此答案至少为 1。

#include "bits/stdc++.h"char mp[1007][1007];int vis[1007][1007], n, m, ans;const int dir[] = {0, 1, 0, -1, 0};bool check_boom(int x, int y) { // 检查 {x, y} 是否有电弧for (int i = 0; i < 4; ++ i) {int nx = x + dir[i], ny = y + dir[i + 1];if (mp[nx][ny] == '#') return false;}return true;
}bool check(int x, int y) {return x >= 1 and x <= n and y >= 1 and y <= m and mp[x][y] != '#' and check_boom(x, y) and vis[x][y] == 0;
}void bfs(int x, int y) {// . and 电弧std::set<std::pair<int, int>> set_a;std::set<std::pair<int, int>> set_b;// now {x, y}std::queue <std::pair<int, int>> q;q.push({x, y});while (!q.empty()) {auto top = q.front(); q.pop();set_a.insert(top);for (int i = 0; i < 4; ++ i) {int nx = top.first + dir[i], ny = top.second + dir[i + 1]; // next_x, next_yif (check(nx, ny)) {q.push({nx, ny});vis[nx][ny] = 1;}}}// 遍历 . 的 set,检查其周围是否有 电弧for (auto &p : set_a) {for (int i = 0; i < 4; ++ i) {int nx = p.first + dir[i], ny = p.second + dir[i + 1];if (mp[nx][ny] == '.' and vis[nx][ny] == 0) set_b.insert({nx, ny});}}   int num = set_a.size() + set_b.size();    ans = std::max(ans, num);
}signed main() {std::cin >> n >> m;for (int i = 1; i <= n; ++ i) {for (int j = 1; j <= m; ++ j) {std::cin >> mp[i][j];}}for (int i = 1; i <= n; ++ i) {for (int j = 1; j <= m; ++ j) {if (vis[i][j] == 0 and mp[i][j] == '.') {ans = std::max(1, ans); // 只要有一个位置为 . ,答案就至少为1if (check_boom(i, j)) bfs(i, j);}}}std::cout << ans;
}

E.夜游江滩

定义 d p [ i ] dp[i] dp[i]表示从 0 0 0走到 i i i的方案数。

初始化 d p [ 0 ] = 1 dp[0] = 1 dp[0]=1

状态转移方程为: d p [ i ] = ∑ j = 0 [ k j ≤ i ] d p [ j ] dp[i] = \sum_{j=0}[k^j \le i]dp[j] dp[i]=j=0[kji]dp[j]

最后输出 d p [ n ] dp[n] dp[n]

每次转移时间复杂度为 O ( l o g ( i ) ) O(log(i)) O(log(i)),于是总复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

注意特判 k = 1 k = 1 k=1的情况。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 9;
ll dp[N];
const ll p = 1e9 + 7;void solve()
{int n, k;cin >> n >> k;if(k == 1){cout << 1 << '\n';return;}dp[0] = 1;for (int i = 1; i <= n; ++i){dp[i] = 0;}for (int i = 1; i <= n; ++i){for (ll j = 1; j <= i; j *= k){dp[i] = (dp[i] + dp[i - j]) % p;}}cout << dp[n] << "\n";
}int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int _;cin >> _;while (_--)solve();return 0;
}

这篇关于StarryCoding 入门教育赛 3 题解C++的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/978727

相关文章

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.