AtCoder Beginner Contest 417

文章目录

    • A A Substring
    • B Search and Delete
    • C Distance Indicators
    • D Takahashi's Expectation
    • E A Path in A Dictionary
    • F Random Gathering
    • G Binary Cat

AtCoder Beginner Contest 417

A A Substring

You are given an N-character string S consisting of lowercase English letters.
Output the string of N−A−B characters obtained by removing the first A characters and the last B characters from S.

翻译:

给定一个由 N 个小写英文字母组成的字符串 S。
输出 S 字符串移除前 A个字符,后 B 个字符后的字符串。

分析:使用string中的 s.erase(pos, k) 删除 s中的从 pos 下标开始的连续 k个元素。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, inf = 0x3f3f3f3f;void solve() {int n, a, b;string s;cin >> n >> a >> b >> s;s.erase(0, a);s.erase(s.size()-b, b); cout << s;
}int main() {int T = 1; while (T--) solve();return 0;
}

B Search and Delete

Takahashi has an integer sequence A=(A1,A2,…,AN)A=(A_1,A_2,…,A_N)A=(A1,A2,,AN) of length N.
It is guaranteed that A is non-decreasing.

Takahashi performs M operations on this integer sequence.
In the i-th operation (1≤i≤M)(1\le i\le M)(1iM), he performs the following operation:

If the sequence A contains BiB_iBi as an element, select one such element and delete it. If no such element exists, do nothing.
Note that since A is non-decreasing, the sequence after the operation is uniquely determined regardless of the choice of element, and remains non-decreasing.

Find A after performing M operations.

翻译:

高桥有一个长度为 N 的整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,…,A_N)A=(A1,A2,,AN)
保证 A 是非递减的。

高桥对这个整数序列执行 M 次操作。在第 i (1≤i≤M)(1\le i\le M)(1iM) 次操作中,他执行以下操作:

如果序列 A 包含 BiB_iBi 作为元素,选择其中一个这样的元素并删除它。如果不存在这样的元素,则不执行任何操作。
注意,由于 A 是非递减的,操作后的序列无论选择哪个元素都是唯一确定的,并且仍然保持非递减。

执行 M 次操作后找到 A 。

分析:数据范围很小,直接暴力模拟即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, inf = 0x3f3f3f3f;void solve() {int n, m;cin >> n >> m;vector<int> a(n + 1), b(m + 1);for (int i = 1; i <= n; i++) cin >> a[i];for (int i = 1; i <= m; i++) cin >> b[i];for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (b[i] == a[j]) {a[j] = -1;break;}}}for (int i = 1; i <= n; i++) {if (a[i] != -1)cout << a[i] << ' ';}
}int main() {int T = 1; while (T--) solve();return 0;
}

C Distance Indicators

You are given an integer sequence A=(A1,A2,...AN)A=(A_1,A_2,...A_N)A=(A1,A2,...AN) of length N.

Find how many pairs of integers (i,j) (1≤i<j≤N1\le i<j \le N1i<jN) satisfy j−i=Ai−Ajj-i=A_i-A_jji=AiAj.

Constraints:1≤N≤2×105,1≤Ai≤2×105(1≤i≤N)1\le N\le 2\times 10^5,1 \le A_i \le 2\times 10^5(1\le i\le N)1N2×105,1Ai2×105(1iN)

翻译:

你被给定一个长度为 N 的整数序列 A=(A1,A2,...AN)A=(A_1,A_2,...A_N)A=(A1,A2,...AN)

找出有多少对整数 (i,j) (1≤i<j≤N1\le i<j \le N1i<jN) 满足 j−i=Ai−Ajj-i=A_i-A_jji=AiAj

约束:1≤N≤2×105,1≤Ai≤2×105(1≤i≤N)1\le N\le 2\times 10^5,1\le A_i \le 2\times 10^5(1\le i\le N)1N2×105,1Ai2×105(1iN)

分析:数据范围较大,枚举复杂度为 O(n2)O(n^2)O(n2),考虑将原式变形:移项得 j−aj=i+ai≥2j-a_j =i+a_i \ge 2jaj=i+ai2,可以发现左右两边均是与当前位置有关的信息。

使用标记数组 st[i+a[i]]++st[i + a[i]] ++st[i+a[i]]++ 表示 i+a[i]i+a[i]i+a[i] 出现的次数加一。

由于答案的更新在 st 更新前,所以保证了 j<ij<ij<i

答案需要开 long long。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, inf = 0x3f3f3f3f;void solve() {ll n, ans = 0;cin >> n;vector<int> a(n + 1), st(N << 1, 0);for (int i = 1; i <= n; i++) {cin >> a[i];if (i - a[i] >= 2)ans += st[i - a[i]];st[i + a[i]]++;}cout << ans << "\n";
}
int main() {int T = 1; while (T--) solve();return 0;
}

D Takahashi’s Expectation

翻译:

分析:

 

E A Path in A Dictionary

You are given a simple connected undirected graph G with N vertices and M edges.
The vertices of G are numbered vertex 1, vertex 2, …, vertex N, and the i-th (1≤i≤M)(1\le i \le M)(1iM) edge connects vertices UiU_iUi and ViV_iVi.

Find the lexicographically smallest simple path from vertex X to vertex Y in G.
That is, find the lexicographically smallest among the integer sequences P=(P1,P2,…,P∣P∣)P=(P1 ,P2 ,…,P_{∣P∣})P=(P1,P2,,PP) that satisfy the following conditions:

  • 1≤Pi≤N1 \le P_i \le N1PiN
  • If i≠ji\neq ji=j, then Pi≠PjP_i\neq P_jPi=Pj.
  • P1=XP_1=XP1=X and P∣P∣=YP_{∣P∣}=YPP=Y.
  • For 1≤i≤∣P∣−11\le i\le ∣P∣−11i≤∣P1, there exists an edge connecting vertices PiP_iPi and Pi+1P_{i+1}Pi+1.

One can prove that such a path always exists under the constraints of this problem.

You are given T test cases, so find the answer for each.

翻译:
给定一个简单的连通无向图 G ,它有 N 个顶点和 M 条边。
G 的顶点编号为顶点 1、2 、… 、N ,第 i 条 (1≤i≤M)(1\le i \le M)(1iM) 边连接顶点 UiU_iUiViV_iVi
​在 G 中,找到从顶点 X 到顶点 Y 的字典序最小的简单路径。
也就是说,找到满足以下条件的整数序列 P=(P1,P2,…,P∣P∣)P=(P1 ,P2 ,…,P_{∣P∣})P=(P1,P2,,PP) 中字典序最小的序列:

  • 1≤Pi≤N1 \le P_i \le N1PiN
  • 如果 i≠ji\neq ji=j, 那么 Pi≠PjP_i\neq P_jPi=Pj.
  • P1=XP_1=XP1=XP∣P∣=YP_{∣P∣}=YPP=Y.
  • 对于 1≤i≤∣P∣−11\le i\le ∣P∣−11i≤∣P1, 存在一条边连接顶点 PiP_iPiPi+1P_{i+1}Pi+1.

可以证明,在问题的约束条件下,这样的路径总是存在。
给出了 T 个测试用例,所以找出每个的答案。

分析:建图后按节点升序排序,dfs 遍历即可。

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, inf = 0x3f3f3f3f, inf2 = 1e18;
vector<int> G[N];
int a[N], n, m, x, y, p;
bool st[N], flag;void dfs(int u) {if (flag) return;st[u] = 1, a[++p] = u;if (u == y) {for (int i = 1; i <= p; i++)cout << a[i] << " \n"[i == p];flag = 1; return;}for (auto& v : G[u]) {if (st[v]) continue;dfs(v);}--p;
}
void solve() {cin >> n >> m >> x >> y;p = 0, flag = 0;memset(st, 0x00, sizeof st);for (int i = 1; i <= n; i++) G[i].clear();for (int i = 1, u, v; i <= m; i++) {cin >> u >> v;G[u].push_back(v), G[v].push_back(u);}for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end());dfs(x);
}
signed main() {int T = 1;cin >> T;while (T--) solve();return 0;
}

F Random Gathering

G Binary Cat

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/94180.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/94180.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++23 Concepts:用类型约束重构泛型编程的终极方案

一、开篇:模板元编程的"类型检查困局" 某金融量化团队曾遇到诡异bug: template<typename T> void process(T data) {static_assert(std::is_arithmetic<T>::value, "需要数值类型");// 业务逻辑... } 当调用process("hello")时…

【RK3568 看门狗驱动开发详解】

RK3568 看门狗驱动开发详解一、Linux 看门狗子系统架构​二、设备树配置​三、 看门狗驱动实现四、验证看门狗定时器&#xff08;Watchdog Timer&#xff09;是保障嵌入式系统可靠性的关键硬件&#xff0c;它通过定期接收 “喂狗” 信号监控系统运行状态&#xff0c;当系统故障…

探索 Vue 3.6 新特性:Vapor Mode 与高性能 Web 应用开发

Vue 3.6 简介 Vue.js 是一个广受欢迎的渐进式 JavaScript 框架&#xff0c;以其简洁的 API、灵活的组件系统和高性能著称。Vue 3.6 是 Vue 3 系列的一个重要版本&#xff0c;引入了多项性能优化和新特性&#xff0c;尤其是备受关注的 Vapor Mode&#xff0c;这是一个无需虚拟 D…

初识prometheus

Prometheus&#xff1a;云原生时代的监控利器 在当今快速发展的云原生和微服务架构时代&#xff0c;传统的监控系统面临着巨大的挑战&#xff1a;如何高效地收集海量、动态变化的指标&#xff1f;如何实时告警并快速定位问题&#xff1f;如何实现灵活的可视化和强大的数据查询…

从源码角度分析导致 JVM 内存泄露的 ThreadLocal

文章目录1. 为什么需要ThreadLocal2. ThreadLocal的实现解析1.1 实现分析1.2 具体实现1.3 ThreadLocalMap中Hash冲突的解决1.3.1 Hash冲突解决的几种方法1.3.1.1 开放定值法1.3.1.2 链地址法1.3.1.3再哈希法&#xff1a;1.3.1.4 建立公共溢出区1.3.2 ThreadLocal解决Hash冲突的…

React组件化的封装

1. 组件化封装的结构 1.1. 定义一个类(组件名必须是大写&#xff0c;小写会被认为是html元素), 继续自React.Component1.2. 实现当前组件的render函数 render当中返回的jsx内容&#xff0c;就是之后React会帮助我们渲染的内容 1.3. 结构图如下&#xff1a; data 方法render()…

嵌入式仿真教学的革新力量:深圳航天科技创新研究院引领高效学习新时代

嵌入式系统作为现代信息技术的核心基石&#xff0c;已深度融入工业控制、物联网、智能终端等关键领域。高校肩负着培养嵌入式技术人才的重任&#xff0c;但传统教学方式正面临严峻挑战&#xff1a;硬件实验设备投入巨大、更新滞后、维护繁琐、时空限制严格&#xff0c;难以满足…

六、Linux核心服务与包管理

作者&#xff1a;IvanCodes 日期&#xff1a;2025年8月3日 专栏&#xff1a;Linux教程 要保证一个Linux系统稳定、安全、功能完备&#xff0c;有效管理其后台服务和软件包是至关重要的。本文将深入介绍现代Linux系统中四个核心的管理工具&#xff1a;systemctl (服务管理)&…

【数据结构】哈希表实现

目录 1. 哈希概念 2 哈希冲突和哈希函数 3. 负载因子 4. 将关键字转为整数 5. 哈希函数 5.1直接定址法 5.2 除法散列法/除留余数法 5.3 乘法散列法&#xff08;了解&#xff09; 5.4 全域散列法&#xff08;了解&#xff09; 5.5 其他方法&#xff08;了解&#xff09…

PostgreSQL面试题及详细答案120道(21-40)

《前后端面试题》专栏集合了前后端各个知识模块的面试题&#xff0c;包括html&#xff0c;javascript&#xff0c;css&#xff0c;vue&#xff0c;react&#xff0c;java&#xff0c;Openlayers&#xff0c;leaflet&#xff0c;cesium&#xff0c;mapboxGL&#xff0c;threejs&…

数据建模及基本数据分析

目录 &#xff08;一&#xff09;数据建模 1.以数据预测为核心的建模 2.以数据聚类为核心的建模 &#xff08;二&#xff09;基本数据分析 1.Numpy 2. Pandas 3.实例 4.Matplotlib 资料自取&#xff1a; 链接: https://pan.baidu.com/s/1PROmz-2hR3VCTd6Eei6lFQ?pwdy8…

电动汽车DCDC转换器的用途及工作原理

在电动汽车的电气架构中&#xff0c;DCDC转换器&#xff08;直流-直流转换器&#xff09;是一个至关重要的部件&#xff0c;负责协调高压动力电池&#xff08;通常300V~800V&#xff09;与低压电气系统&#xff08;12V/24V&#xff09;之间的能量流动。它的性能直接影响整车的能…

PyTorch 应用于3D 点云数据处理汇总和点云配准示例演示

PyTorch 已广泛应用于 3D 点云数据处理&#xff0c;特别是在深度学习驱动的任务中如&#xff1a; 分类、分割、配准、重建、姿态估计、SLAM、目标检测 等。 传统 3D 点云处理以 PCL、Open3D 为主&#xff0c;深度学习方法中&#xff0c;PyTorch 是构建神经网络处理点云的核心框…

ABP VNext + Quartz.NET vs Hangfire:灵活调度与任务管理

ABP VNext Quartz.NET vs Hangfire&#xff1a;灵活调度与任务管理 &#x1f680; &#x1f4da; 目录ABP VNext Quartz.NET vs Hangfire&#xff1a;灵活调度与任务管理 &#x1f680;✨ TL;DR&#x1f6e0; 环境与依赖&#x1f527; Quartz.NET 在 ABP 中接入1. 安装与模块…

[硬件电路-148]:数字电路 - 什么是CMOS电平、TTL电平?还有哪些其他电平标准?发展历史?

1. CMOS电平定义&#xff1a; CMOS&#xff08;Complementary Metal-Oxide-Semiconductor&#xff09;电平基于互补金属氧化物半导体工艺&#xff0c;由PMOS和NMOS晶体管组成。其核心特点是低功耗、高抗干扰性和宽电源电压范围&#xff08;通常为3V~18V&#xff09;。关键参数&…

0基礎網站開發技術教學(二) --(前端篇 2)--

書接上回說到的前端3種主語言以及其用法&#xff0c;這期我們再來探討一下javascript的一些編碼技術。 一) 自定義函數 假如你要使用一個功能&#xff0c;正常來說直接敲出來便可。可如果這個功能你要用不止一次呢?難道你每次都敲出來嗎?這個時侯&#xff0c;就要用到我們的自…

前端 拼多多4399笔试题目

拼多多 3 选择题 opacity|visibity|display区别 在CSS中&#xff0c;opacity: 0 和 visibility: hidden 都可以让元素不可见&#xff0c;但它们的行为不同&#xff1a; ✅ opacity: 0&#xff08;透明度为0&#xff09; 元素仍然占据空间&#xff08;不移除文档流&#xff0…

数琨创享:全球汽车高端制造企业 QMS质量管理平台案例

01.行业领军者的质量升级使命在全球汽车产业链加速升级的浪潮中&#xff0c;质量管控能力已成为企业核心竞争力的关键。作为工信部认证的制造业单项冠军示范企业&#xff0c;万向集团始终以“全球制造、全球市场、做行业领跑者”为战略愿景。面对奔驰、宝马、大众等“9N”高端客…

GaussDB 约束的使用举例

1 not null 约束not null 约束强制列不接受 null 值。not null 约束强制字段始终包含值。这意味着&#xff0c;如果不向字段添加值&#xff0c;就无法插入新记录或者更新记录。GaussDB使用pg_get_tabledef()函数获取customers表结构&#xff0c;如&#xff1a;csdn> set sea…

自动驾驶中的传感器技术13——Camera(4)

1、自驾Camera开发的方案是否归一化对于OEM&#xff0c;或者自驾方案商如Mobileye如果进行Camera的开发&#xff0c;一般建议采用Tesla的系统化最优方案&#xff0c;所有Camera统一某个或者某两个MP设计&#xff08;增加CIS议价权&#xff0c;减少Camera PCBA的设计维护数量&am…