当前位置: 首页 > news >正文

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.lryc.cn/news/608940.html

相关文章:

  • [硬件电路-147]:模拟电路 - DC/DC电压的三种架构:升压(Boost)、降压(Buck)或升降压(Buck-Boost)
  • 跨语言模型中的翻译任务:XLM-RoBERTa在翻译任务中的应用
  • 界面规范4-按钮
  • IntelliJ IDEA开发编辑器摸鱼看股票数据
  • Parcel 使用详解:零配置的前端打包工具
  • 关于车位引导及汽车乘梯解决方案的专业性、系统性、可落地性强的综合设计方案与技术实现说明,旨在为现代智慧停车楼提供高效、安全、智能的停车体验。
  • electron-多线程
  • 嵌入式——数据结构:单向链表的函数创建
  • 常见的深度学习模块/操作中的维度约定(系统性总结)
  • Docker-03.快速入门-部署MySQL
  • 介绍JAVA语言、介绍greenfoot 工具
  • 北邮:LLM强化学习架构Graph-R1
  • 【机器学习】线性回归算法详解:线性回归、岭回归、Lasso回归与Elastic Net
  • 02.Redis 安装
  • 13.Redis 的级联复制
  • kafka与其他消息队列(如 RabbitMQ, ActiveMQ)相比,有什么优缺点?
  • 《深入浅出RabbitMQ:从零基础到面试通关》
  • RabbitMQ面试精讲 Day 10:消息追踪与幂等性保证
  • 《软件测试与质量控制》实验报告三 系统功能测试
  • Flutter开发 dart异步
  • Spring lookup-method实现原理深度解析
  • [spring-cloud: 服务注册]-源码解析
  • 【Linux】linux基础开发工具(三) 版本控制器Git、调试器 - gdb/cgdb使用、一些实用的调试技巧
  • graph TD的规则
  • zookeeper持久化和恢复原理
  • 大模型智能体(Agent)技术全景:架构演进、协作范式与应用前沿
  • io_destroy系统调用及示例
  • Redis——运维篇
  • Linux | i.MX6ULL移植 Gdb+Gdbserver 调试(第十四章)
  • day50预训练模型 CBAM注意力