河工院首届工业设计大赛程序组(挑战赛)题解

news/2024/9/17 19:04:47/ 标签: cpp, 算法

更好的阅读体验 \huge{\color{red}{更好的阅读体验}} 更好的阅读体验

寻找ACMer


思想:

  • 签到题
  • 按照题意遍历字符串,不断向后寻找包含 ACMer 完整字符串的数量即可

std标程:

cpp">#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>using namespace std;#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);void solve(){string s; cin >> s;int idx = 0;int ans = 0;while(s.find("ACMer", idx) != -1) {ans ++; idx = s.find("ACMer", idx) + 1;}cout << ans << endl;}int main(){IOS;int _ = 1;// cin >> _;while(_ --){solve();}return 0;}

欧拉幻树苗


思想:

  • 树形DP
  • 始化每一个节点为独立的连通分量,即每个节点自身就是一个树的根。
  • 读取树的结构,确保我们可以通过 g 数组访问到每个节点的孩子节点。
  • 读取特殊边,并使用并查集合并特殊边的两个端点。由于题目保证特殊边的两个端点深度相同,合并这些端点不会导致环的出现。
  • 然后开始广度优先搜索。从根节点(节点1)开始,用队列来记录接下来需要访问的节点。
  • 对于当前节点 t,如果它是叶子,将 find(t) 的路径数加到答案中(即cnt[find(t)]),因为从叶子节点可以直接走到根节点。
  • 遍历当前节点t的所有孩子节点,将父节点到当前节点的路径数累加到孩子节点上(需要通过find函数找到孩子节点所在的连通分量),然后将这些孩子节点加入队列中以进行下一轮搜索。
  • 当队列为空时,所有节点都被访问过,搜索结束。最后输出计算的答案 ans。

std标程:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>using namespace std;#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;const int N   =   1e6  +  3;
const int INF   =   0x3f3f3f3f, mod   =   1e9  +  7;
const double eps   =   1e-6, PI   =   acos(-1);int f[N], cnt[N];  // f数组用于并查集,cnt数组用于记录路径数vector<int> g[N];  // g数组用来存储树的结构int find(int x){return f[x] == x ? x : f[x]  =  find(f[x]);
}void solve() {int n, m; cin >> n >> m;for(int i = 1; i <= n; i ++) f[i] = i; // 初始化for(int i = 1; i < n; i ++){int x, y; cin >> x >> y; // 读取树的结构g[y].push_back(x); // 假设每条边是从子节点指向父节点}for(int i = 1; i <= m; i ++){int x, y; cin >> x >> y; // 输入特殊边f[find(x)] = find(y); // 合并特殊边的端点}queue<int> q;q.push(1);cnt[1] = 1; // 根节点的路径数为1int ans = 0; // 结果变量while(!q.empty()){int t = q.front();q.pop();if(g[t].empty()) (ans += cnt[find(t)]) %= mod; // 如果t是叶子节点,累加路径数for(auto i:g[t]){(cnt[find(i)] += cnt[find(t)]) %= mod; // 将当前节点的路径数累加到其孩子节点q.push(i); // 将孩子节点加入队列}}cout << ans; 
}int main(){IOS;int _   =   1;//cin >> _;while(_ --){solve();}return 0;
}

疯狂蓝桥杯

本题主要考察四舍五入,C语言中是四舍六入,但是需要四舍五入,则在结果后面加上0.001即可。

std标程:

cpp">#include<bits/stdc++.h>using namespace std;
#define int long long 
signed main()
{int T;cin>>T;while(T--){int n,m,x,y;cin>>n>>m>>x>>y;int z=n*y,zz=m*x;int zzz=z*zz/__gcd(z,zz);int i=zzz/z,j=zzz/zz;double s=sqrt(n*n*i*i+m*m*j*j) * 2; s+=0.001;printf("%.2lf\n",s);} return 0;
} 

Inception Ⅰ

用数组(如st)记录每个数字出现的次数,从1枚举到 (m + 1)/ 2 (不含),所有 st[i] * st[m - i]的和,注意要开long long~

std标程:

#include <iostream>using namespace std;const int N = 1e6 + 20;
int n, m, st[N];
long long ans;int main()
{int x;cin >> n >> m;for(int i = 1; i <= n; i ++){cin >> x;st[x] ++;}for(int i = 0; i < m - i; i ++){ans += (long long)st[i] * st[m - i];}cout << ans;return 0;
}

Inception Ⅱ

循环枚举判断是否存在连续三个字母/四个元素组成的回文数组,(易证所有长度大于2的回文都包含这两种回文之一),定义数组记录下标为 i 的元素向后找最近的回文数组右端点的距离,如 1 1 2 2 1,对应记录数组应为 4,3,0,0,0。对每次查找的时间复杂度缩小到O(1)。

std标程:

cpp">#include <iostream>using namespace std;const int N = 1e6 + 20;
int n, q[N], s[N];int main()
{int t;scanf("%d %d", &n, &t);for (int i = 1; i <= n; i ++){scanf("%d", &q[i]);}for (int i = 1; i <= n; i ++){//偶数回文if (i + 3 <= n){if(q[i] == q[i + 3] && q[i + 1] == q[i + 2]) s[i] = 3;}//奇数回文if (i + 2 <= n){if (q[i] == q[i + 2]) s[i] = 2;}}int f = 0;for (int i = n; i > 0; i --){if (s[i] && !f) f = 1;if (f && !s[i]) s[i] = s[i + 1] + 1;}int l, r;while(t --){scanf("%d %d", &l, &r);if (!s[l]) printf("NO\n");else if (l + s[l] <= r) printf("YES\n");else printf("NO\n");}return 0;
}

Inception Ⅲ

易证,当 n == 1 或 n == 2 时,图腾陀螺可一步取胜,除此之外,Belinra均可胜利(想什么呢,这可是Belinra的主场,给你先手是客套,怎么可能让你轻易取胜)

std标程:

cpp">#include <iostream>using namespace std;int main()
{int n; cin >> n;if (n == 1 || n == 2) cout << "None life is but a dream .";else cout << "Wake up now !";return 0;
}

憧憬成为AK大神


思想:

  • 贪心
  • 有题目可知,到了某一个点都不能可能再往回走(回头一定不是最优解,否则在原来就已经进去AK了)
  • 先对页面编号排序,然后用一个大根堆来维护从起始页面切换到当前这个页面的已AK的场次所消耗的时间
  • 如果所有的时间消耗(切换界面的时间+对应场次让出的时间)已大于规定的时间,则该方向上的时间不可避免
  • 所以只能少切换界面,因为每一场比赛都AK一次,即将让出时间最大的页面跳过即可

std标程:

cpp">#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>using namespace std;#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);priority_queue<LL> q;PLL a[N];bool cmp(PLL &a, PLL b) {return a.fi < b.fi;
}LL n, m, ans, res, sum, idx;void solve(){cin >> n >> m;for (int i = 0; i <= n; i ++) {LL x, y; cin >> x >> y;a[++ idx].fi = x;a[idx].se = y;}sort(a + 1, a + n + 1, cmp);for(int i = 1; i <= n; i ++) {res += a[i].fi - a[i - 1].fi; //切换到 i 页面所用时间 q.push(a[i].se); // sum 的欲望 sum ++;res += a[i].se;while(!q.empty() && res > m) //如果用的时间多于m,直接ak掉 {sum --;res -= q.top();q.pop();}if(res > m) break; ans = max(ans, sum); }cout << ans << endl;
}int main(){IOS;int _ = 1;// cin >> _;while(_ --){solve();}return 0;}

高精度拼接

由于每添加一个数都需要满足 a % b == 0,故第一次从 0 到 9 枚举,如果存在满足条件的数直接输出即可,紧接着输出 n - 1 个 0,否则输出 -1

std标程:

cpp">#include <stdio.h>int main()
{int a, b, c, ans = -1;scanf("%d %d %d", &a, &b, &c);for (int i = 0; i <= 9; i ++){if ((a * 10 + i) % b == 0){ans = a * 10 + i;break;}}if (ans != -1){printf("%d", ans);for (int i = 1; i <= c - 1; i ++)printf("0");}else{printf("-1");}return 0;
}

逆矩阵


  • 签到题,模拟

std标程:

cpp">#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>using namespace std;#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);void solve(){int n; cin >> n;int size = n;n *= n;vector<vector<int>> ans(size, vector<int>(size, 0));int num = 1;int top = 0, st = size - 1, left = 0, right = size - 1;while(num <= n) {for(int i = top; i <= st && num <= n; i++) {ans[i][left] = num++;}left++; for(int i = left; i <= right && num <= n; i++) {ans[st][i] = num++;}st--;for(int i = st; i >= top && num <= n; i--) {ans[i][right] = num++;}right--;for(int i = right; i >= left && num <= n; i--) {ans[top][i] = num++;}top++;}for (int i = 0; i < size; i++) {for (int j = 0; j < size; j++) {if (ans[i][j] != 0) {cout << ans[i][j] << " ";}}cout << endl;}
}int main(){IOS;int _ = 1;// cin >> _;while(_ --){solve();}return 0;}

http://www.ppmy.cn/news/1507069.html

相关文章

人工智能时代,程序员如何保持核心竞争力?

人工智能时代&#xff0c;程序员如何保持核心竞争力&#xff1f; 在人工智能的浪潮中&#xff0c;程序员的角色和工作方式正在经历前所未有的变革。AIGC技术的兴起&#xff0c;如ChatGPT、Midjourney、Claude等&#xff0c;预示着AI辅助编程工具的日益普及。面对这一趋势&…

海量日志数据收集监控平台应该怎么设计和实现

设计和实现一个海量日志数据收集和监控平台&#xff0c;需要考虑以下几个关键方面&#xff1a;数据采集、数据存储、实时处理、监控与告警、可视化分析、扩展性和高可用性。以下是一个详细的设计和实现方案&#xff1a; 1. 需求分析 日志来源&#xff1a;明确日志的来源&…

图谱驱动的智能:如何用Django实现GraphRAG的高效检索

前言 前面一章讲述了构建知识图谱来提高基于 RAG 的应用程序的准确性,并且使用 Neo4j 和 LangChain 在 RAG 应用程序中构建和检索知识图谱信息。 图形检索增强生成 (Graph RAG) 这种方法利用图形数据库的结构化特性,将数据组织为节点和关系,以增强检索信息的深度和上下文性…

HDFS写入数据的流程图

1.客户端向namenode发送请求&#xff0c;请示写入数据 2.namenode接受请求后&#xff0c;判断这个用户是否有写入权限&#xff0c;如果不具备直接报错&#xff1b;如果有写入权限&#xff0c;接着判断在要写入的目录下是否已经存在这个文件&#xff0c;如果存在&#xff0c;直…

Ubuntu gnome WhiteSur-gtk-theme类mac主题正确安装和卸载方式

目录 摘要目的安装和卸载特别说明 Ubuntu gnome WhiteSur-gtk-theme类mac主题正确安装和卸载方式 摘要 Ubuntu版本&#xff1a;ubuntu24.04 主题下载地址&#xff1a;https://github.com/vinceliuice/WhiteSur-gtk-theme 参考的安装教程&#xff1a;https://blog.51cto.com/u_…

Mybatis的详细讲解

1.前情提要 1.1三层架构 &#xff08;1&#xff09;表现层 Controller 表现层是表示的事数据的接受&#xff0c;参数的校验&#xff0c;参数的转换&#xff0c;结果的转换&#xff0c;结果的返回 &#xff08;2&#xff09;业务逻辑层 Service 介于表现层和业务逻辑层之…

使用Cisco软件进行模拟万维网配置访问服务器过程

万维网(www)实验 文章目录 万维网(www)实验1.实验目的2.实验流程3.实验步骤 1.实验目的 1&#xff09;理解www站点 2&#xff09;理解上层应用和下层通信网络的关系 2.实验流程 开始 → 布置拓扑 → 配置路由及IP地址 → 配置web服务器→ 访问服务器 →结束 3.实验步骤 1&…

Vercel Error: (Azure) OpenAI API key not found

题意&#xff1a;Vercel 错误&#xff1a;(Azure) OpenAI API 密钥未找到 问题背景&#xff1a; I implemented openAI API in my Next.js app with the help of langchain library and it works superb on localhost, but in Vercel (ProVersion) it throws an error: 我使用…

js函数的arguments 对象

arguments对象是函数中传递的参数值的集合。 它是⼀个类似数组的对象&#xff0c;因为它有⼀个length属性&#xff0c; 我们可以使⽤数组索引表示法arguments[1]来访问单个值&#xff0c;但它没有数组中的内置⽅法&#xff0c; 如&#xff1a;forEach、reduce、filter和map。 …

AI的IDE:Cursor配置虚拟python环境(conda)

AI的IDE&#xff1a;Cursor配置虚拟python环境&#xff08;conda&#xff09; Cursor是一个AI的IDE&#xff0c;是从VSCode源代码中fork出来的&#xff0c;专注于和AI一起Coding而生。https://www.cursor.com/是官方地址。最近开始逐渐的试用Cursor&#xff0c;之前一直是VSCod…

机器学习之随机森林

文章目录 1. 随机森林概述1.1 定义与起源1.2 与其他算法的比较 2. 随机森林的工作原理2.1 决策树基础2.2 Bagging机制2.3 随机性的引入 3. 随机森林的构建过程3.1 数据准备3.2 特征选择3.3 多棵树的集成 4. 随机森林的优缺点分析4.1 优势4.2 局限性 5. 随机森林的应用场景5.1 分…

Kali Linux——网络安全的瑞士军刀

一、引言 在网络安全的领域中&#xff0c;Kali Linux 宛如一把强大而全能的瑞士军刀&#xff0c;为安全研究人员和专业人士提供了丰富的工具和资源。本文将深入探讨 Kali Linux 的特点、优势、常用工具以及实际应用场景&#xff0c;带您领略这一强大操作系统的魅力。 二、Kal…

OpenCV||超详细的图像金字塔

图像金字塔是一种图像的多尺度表示方法&#xff0c;它通过对原始图像进行一系列的处理&#xff0c;生成一系列分辨率逐渐降低的图像集合。这些图像按照分辨率从高到低&#xff08;或从低到高&#xff09;的顺序排列&#xff0c;形成类似金字塔的结构&#xff0c;因此得名图像金…

Basic‘ attribute type should not be a container解决方法

在使用Spring Data JPA的时候&#xff0c;实体类中定义一个用List修饰的成员ip&#xff0c;IDEA会提示Basic‘ attribute type should not be a container错误&#xff0c;导致编译不通过。 查阅一些博客和文档说是Spring Data JPA这个框架会把实体类的属性当做是MySQL数据库中…

深入理解Transformer技术原理

一、什么是注意力机制 在深入了解Transformer的架构原理之前&#xff0c;我们首先要了解下&#xff0c;什么是注意力机制。人类的大脑对于信息的获取也存在注意力机制&#xff0c;下面我举几个简单的例子&#xff1a;从上面的图片中&#xff0c;我们可能更容易关注&#xff0c…

【Next】全局样式和局部样式

不同于 nuxt &#xff0c;next 的样式绝大部分都需要手动导入。 全局样式 使用 sass 先安装 npm i sass -D 。 我们可以定义一个 styles 文件&#xff0c;存放全局样式。 variables.scss $fs30: 30px;mixin border() {border: 1px solid red; }main.scss use ./variables …

APP UI自动化测试框架有哪些

UI&#xff08;用户界面&#xff09;自动化测试是软件测试中的一种重要方式&#xff0c;它可以帮助验证应用程序的图形用户界面是否按照预期工作。对于移动应用&#xff08;如iOS和Android应用&#xff09;&#xff0c;有多种自动化测试框架可以选择&#xff0c;它们各有特色&a…

Python爬虫(8)

JsonPath介绍使用 JsonPath是一种轻量级的查询库&#xff0c;可以从JSON文本数据中进行筛选和提取操作。有点类似于使用XPath在HTML数据中提取数据的功能。JsonPath 也可以通过使用类似于 XPath 的表达式来访问 JSON对象中的属性和元素&#xff0c;并支持通配符、筛选器和函数…

NET8环境WebAPI实现文件的压缩及下载

目录 1、文件下载的原理2、具体实现2.1 提前准备2.2 服务器端的实现2.3 请求端的实现 3、代码下载4、更多特性4.1 单独压缩文件4.2 解析4.2.1 整体解析4.2.2 单个文件解析 4.3 其他4.3.1 设置压缩级别4.3.2 密码保护4.3.3 进度反馈 5、参考资料 1、文件下载的原理 在实际应用环…

蜂鸣器(51单片机)

一、蜂鸣器介绍 1.蜂鸣器 2.蜂鸣器电路 3.芯片图示 二、蜂鸣器功能实现 1.蜂鸣器提示音代码 蜂鸣器函数 播放提示音功能实现 2.蜂鸣器播放音乐