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++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

VSCode中C/C++编码乱码问题的两种解决方法

《VSCode中C/C++编码乱码问题的两种解决方法》在中国地区,Windows系统中的cmd和PowerShell默认编码是GBK,但VSCode默认使用UTF-8编码,这种编码不一致会导致在VSC... 目录问题方法一:通过 Code Runner 插件调整编码配置步骤方法二:在 PowerShell

C/C++随机数生成的五种方法

《C/C++随机数生成的五种方法》C++作为一种古老的编程语言,其随机数生成的方法已经经历了多次的变革,早期的C++版本使用的是rand()函数和RAND_MAX常量,这种方法虽然简单,但并不总是提供... 目录C/C++ 随机数生成方法1. 使用 rand() 和 srand()2. 使用 <random

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定