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)(1≤i≤M), 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)(1≤i≤M) 次操作中,他执行以下操作:
如果序列 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 N1≤i<j≤N) satisfy j−i=Ai−Ajj-i=A_i-A_jj−i=Ai−Aj.
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)1≤N≤2×105,1≤Ai≤2×105(1≤i≤N)。
翻译:
你被给定一个长度为 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 N1≤i<j≤N) 满足 j−i=Ai−Ajj-i=A_i-A_jj−i=Ai−Aj。
约束: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)1≤N≤2×105,1≤Ai≤2×105(1≤i≤N)。
分析:数据范围较大,枚举复杂度为 O(n2)O(n^2)O(n2),考虑将原式变形:移项得 j−aj=i+ai≥2j-a_j =i+a_i \ge 2j−aj=i+ai≥2,可以发现左右两边均是与当前位置有关的信息。
使用标记数组 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)(1≤i≤M) 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,…,P∣P∣) that satisfy the following conditions:
- 1≤Pi≤N1 \le P_i \le N1≤Pi≤N
- If i≠ji\neq ji=j, then Pi≠PjP_i\neq P_jPi=Pj.
- P1=XP_1=XP1=X and P∣P∣=YP_{∣P∣}=YP∣P∣=Y.
- For 1≤i≤∣P∣−11\le i\le ∣P∣−11≤i≤∣P∣−1, 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)(1≤i≤M) 边连接顶点 UiU_iUi 和 ViV_iVi。
在 G 中,找到从顶点 X 到顶点 Y 的字典序最小的简单路径。
也就是说,找到满足以下条件的整数序列 P=(P1,P2,…,P∣P∣)P=(P1 ,P2 ,…,P_{∣P∣})P=(P1,P2,…,P∣P∣) 中字典序最小的序列:
- 1≤Pi≤N1 \le P_i \le N1≤Pi≤N
- 如果 i≠ji\neq ji=j, 那么 Pi≠PjP_i\neq P_jPi=Pj.
- P1=XP_1=XP1=X 且 P∣P∣=YP_{∣P∣}=YP∣P∣=Y.
- 对于 1≤i≤∣P∣−11\le i\le ∣P∣−11≤i≤∣P∣−1, 存在一条边连接顶点 PiP_iPi 和 Pi+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;
}