当前位置: 首页 > 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.dtcms.com/a/313202.html

相关文章:

  • MySQL事务与存储引擎的学习(一)
  • Docker国内镜像列表
  • Effective C++ 条款19: 设计class犹如设计type
  • Python从入门到精通计划Day02: Python语法探秘:当现代艺术遇到古典音乐
  • 最小半径覆盖问题【C++解法+二分+扫描线】
  • 【CF】Day118——杂题 (随机哈希 / 思维 | 贪心 / DP | 位运算构造 | 状态压缩 + 建图 + 最短路 | 构造 | 贪心)
  • 使用纯Docker命令搭建多服务环境(Linux版)
  • Python篇---包
  • 在Ansys Mechanical中对磨损进行建模
  • 力扣经典算法篇-40-螺旋矩阵(方向遍历:方向数组+已访问元素集合)
  • 【ROS2】常用命令
  • 04.Redis 的多实例
  • 双八无碳小车设计【16张cad】三维图+设计说明书
  • 【C++ 初级工程师面试--5】inline内联函数特点 、和普通函数的区别、什么时候适合内联?
  • json-server 快速搭建本地 Mock 数据服务
  • Day23--回溯--39. 组合总和,40. 组合总和 II,131. 分割回文串
  • Android 之 MVC架构
  • 线段树学习笔记 - 摩尔投票问题
  • I2C基础
  • mybatis-plus从入门到入土(四):持久层接口之BaseMapper和选装件
  • PHP现代化全栈开发:前后端分离与API架构实践
  • uni-app学习笔记01-项目初始化及相关文件
  • Go语言常量
  • 11.消息队列
  • 计算机视觉CS231n学习(2)
  • 从马武寨穿越关山
  • ICCV 2025 | EPD-Solver:西湖大学发布并行加速扩散采样算法
  • p5.js 用 beginGeometry () 和 endGeometry () 打造自定义 3D 模型
  • 控制建模matlab练习06:比例积分控制-②PI控制器
  • 达梦数据库联机备份和脱机备份的区别