AtCoder Beginner Contest 418
文章目录
- A I'm a teapot
- B You're a teapot
- C Flush
- D XNOR Operation
- E Trapezium
- F We're teapots
- G Binary Operation
AtCoder Beginner Contest 418
A I’m a teapot
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;void solve() {int n; string s; cin >> n >> s;bool f = (n > 2 && s.substr(n - 3, 3) == "tea");cout << (f ? "Yes" : "No") << "\n";
}
int main() {// freopen("1.in", "r", stdin);int T = 1; while (T--) solve();return 0;
}
B You’re a teapot
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;void solve() {string s; cin >> s;int x = 0, n = s.size();double ans = 0;for (int i = 0; i < n; i++)for (int j = i + 1; j < n; j++)if (s[i] == 't' && s[j] == 't') {int x = 0, len = j - i + 1;for (int k = i; k <= j; k++)x += (s[k] == 't');ans = max(ans, (x - 2.0) / (len - 2.0));}cout << fixed << setprecision(12) << ans << "\n";
}
int main() {// freopen("1.in", "r", stdin);int T = 1; while (T--) solve();return 0;
}
C Flush
On the poker table, there are tea bags of NNN different flavors. The flavors are numbered from 1 through NNN, and there are AiA_iAi tea bags of flavor iii (1≤i≤N1 \leq i \leq N1≤i≤N).
You will play a game using these tea bags. The game has a parameter called difficulty between 1 and A1+⋯+ANA_1 + \cdots + A_NA1+⋯+AN, inclusive. A game of difficulty bbb proceeds as follows:
- You declare an integer xxx. Here, it must satisfy b≤x≤A1+⋯+ANb \leq x \leq A_1 + \cdots + A_Nb≤x≤A1+⋯+AN.
- The dealer chooses exactly xxx tea bags from among those on the table and gives them to you.
- You check the flavors of the xxx tea bags you received, and choose bbb tea bags from them.
- If all bbb tea bags you chose are of the same flavor, you win. Otherwise, you lose.
The dealer will do their best to make you lose.
You are given QQQ queries, so answer each of them. The jjj-th query is as follows:
- For a game of difficulty BjB_jBj, report the minimum integer xxx you must declare at the start to guarantee a win. If it is impossible to win, report −1-1−1 instead.
Constraints
- 1≤N≤3×1051 \leq N \leq 3 \times 10^51≤N≤3×105
- 1≤Q≤3×1051 \leq Q \leq 3 \times 10^51≤Q≤3×105
- 1≤Ai≤1061 \leq A_i \leq 10^61≤Ai≤106 (1≤i≤N1 \leq i \leq N1≤i≤N)
- 1≤Bj≤min(109,A1+⋯+AN)1 \leq B_j \leq \min(10^9, A_1 + \cdots + A_N)1≤Bj≤min(109,A1+⋯+AN) (1≤j≤Q1 \leq j \leq Q1≤j≤Q)
- All input values are integers.
翻译:
在扑克桌上,有不同口味的茶包。口味从 111 到 NNN 编号,有 AiA_iAi 个茶包口味 iii(1≤i≤N1\leq i\leq N1≤i≤N)。
你将用这些茶包玩游戏。游戏有一个名为难度的参数,介于 111 和 a1+⋯+aNa_1+\cdots+a_Na1+⋯+aN 之间。难度游戏 bbb 的收益如下:
- 声明一个整数 xxx。这里,它必须满足 b≤x≤A1+⋯+ANb\leq x\leq A_1+\cdots+A_Nb≤x≤A1+⋯+AN。
- 经销商从桌上的茶包中准确地选择 xxx 个茶包,并将其送给您。
- 您检查收到的 xxx 个茶包,并从中选择 bbb 个茶包。
- 如果你选择的 bbb 个茶包都是相同的味道,你就赢了。否则,你输了。
经销商会尽最大努力让你输。
您会收到 QQQ 的查询,请逐一回答。第 jjj 个查询如下:
- 对于难度为 BjB_jBj 的游戏,报告您必须在开始时声明的最小整数 xxx,以确保获胜。如果不可能获胜,则报告 −1-1−1。
约束条件
- 1≤N≤3×1051 \leq N \leq 3 \times 10^51≤N≤3×105
- 1≤Q≤3×1051 \leq Q \leq 3 \times 10^51≤Q≤3×105
- 1≤Ai≤1061 \leq A_i \leq 10^61≤Ai≤106 (1≤i≤N1 \leq i \leq N1≤i≤N)
- 1≤Bj≤min(109,A1+⋯+AN)1 \leq B_j \leq \min(10^9, A_1 + \cdots + A_N)1≤Bj≤min(109,A1+⋯+AN) (1≤j≤Q1 \leq j \leq Q1≤j≤Q)
- 所有输入值都是整数。
分析:
本题目要模拟样例,找到题目所求:当相同元素的数量至少为 bbb 的需要最少有解数量 xxx。
解法:考虑对原数组 aia_iai 排序,求出前缀和 sis_isi,二分查询第一个≥b\ge b≥b 的元素位置 ididid,如果答案存在,则 ans=sid−1+(b−1)×(n−id+1)+1ans=s_{id-1} + (b-1) \times (n-id+1) +1ans=sid−1+(b−1)×(n−id+1)+1;否则 ans=−1ans=-1ans=−1。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int n, q, a[N], b;
ll s[N];void solve() {cin >> n >> q;for (int i = 1; i <= n; i++) cin >> a[i];sort(a + 1, a + 1 + n);for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];while (q--) {cin >> b;int id = lower_bound(a + 1, a + 1 + n, b) - a;ll ans = -1;if (id <= n && a[id] >= b)ans = s[id - 1] + 1ll * (b - 1) * (n - id + 1) + 1;cout << ans << "\n";}
}
int main() {// freopen("1.in", "r", stdin);int T = 1; while (T--) solve();return 0;
}