完整教程:AtCoder Beginner Contest 418


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 N N N different flavors. The flavors are numbered from 1 through N N N, and there are A i A_i Ai tea bags of flavor i i i ( 1 ≤ i ≤ N 1 \leq i \leq N 1iN).

You will play a game using these tea bags. The game has a parameter called difficulty between 1 and A 1 + ⋯ + A N A_1 + \cdots + A_N A1++AN, inclusive. A game of difficulty b b b proceeds as follows:

  1. You declare an integer x x x. Here, it must satisfy b ≤ x ≤ A 1 + ⋯ + A N b \leq x \leq A_1 + \cdots + A_N bxA1++AN.
  2. The dealer chooses exactly x x x tea bags from among those on the table and gives them to you.
  3. You check the flavors of the x x x tea bags you received, and choose b b b tea bags from them.
  4. If all b b b 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 Q Q Q queries, so answer each of them. The j j j-th query is as follows:

  • For a game of difficulty B j B_j Bj, report the minimum integer x x x 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 × 1 0 5 1 \leq N \leq 3 \times 10^5 1N3×105
  • 1 ≤ Q ≤ 3 × 1 0 5 1 \leq Q \leq 3 \times 10^5 1Q3×105
  • 1 ≤ A i ≤ 1 0 6 1 \leq A_i \leq 10^6 1Ai106 ( 1 ≤ i ≤ N 1 \leq i \leq N 1iN)
  • 1 ≤ B j ≤ min ⁡ ( 1 0 9 , A 1 + ⋯ + A N ) 1 \leq B_j \leq \min(10^9, A_1 + \cdots + A_N) 1Bjmin(109,A1++AN) ( 1 ≤ j ≤ Q 1 \leq j \leq Q 1jQ)
  • All input values are integers.

翻译:
在扑克桌上,有不同口味的茶包。口味从 1 1 1 N N N 编号,有 A i A_i Ai 个茶包口味 i i i 1 ≤ i ≤ N 1\leq i\leq N 1iN)。
你将用这些茶包玩游戏。游戏有一个名为难度的参数,介于 1 1 1 a 1 + ⋯ + a N a_1+\cdots+a_N a1++aN 之间。难度游戏 b b b 的收益如下:

  1. 声明一个整数 x x x。这里,它必须满足 b ≤ x ≤ A 1 + ⋯ + A N b\leq x\leq A_1+\cdots+A_N bxA1++AN
  2. 经销商从桌上的茶包中准确地选择 x x x 个茶包,并将其送给您。
  3. 您检查收到的 x x x 个茶包,并从中选择 b b b 个茶包。
  4. 如果你选择的 b b b 个茶包都是相同的味道,你就赢了。否则,你输了。

经销商会尽最大努力让你输。

您会收到 Q Q Q 的查询,请逐一回答。第 j j j 个查询如下:

  • 对于难度为 B j B_j Bj 的游戏,报告您必须在开始时声明的最小整数 x x x,以确保获胜。如果不可能获胜,则报告 − 1 -1 1

约束条件

  • 1 ≤ N ≤ 3 × 1 0 5 1 \leq N \leq 3 \times 10^5 1N3×105
  • 1 ≤ Q ≤ 3 × 1 0 5 1 \leq Q \leq 3 \times 10^5 1Q3×105
  • 1 ≤ A i ≤ 1 0 6 1 \leq A_i \leq 10^6 1Ai106 ( 1 ≤ i ≤ N 1 \leq i \leq N 1iN)
  • 1 ≤ B j ≤ min ⁡ ( 1 0 9 , A 1 + ⋯ + A N ) 1 \leq B_j \leq \min(10^9, A_1 + \cdots + A_N) 1Bjmin(109,A1++AN) ( 1 ≤ j ≤ Q 1 \leq j \leq Q 1jQ)
  • 所有输入值都是整数。

分析:

本题目要模拟样例,找到题目所求:当相同元素的数量至少为 b b b 的需要最少有解数量 x x x

解法:考虑对原数组 a i a_i ai 排序,求出前缀和 s i s_i si,二分查询第一个 ≥ b \ge b b 的元素位置 i d id id,如果答案存在,则 a n s = s i d − 1 + ( b − 1 ) × ( n − i d + 1 ) + 1 ans=s_{id-1} + (b-1) \times (n-id+1) +1 ans=sid1+(b1)×(nid+1)+1;否则 a n s = − 1 ans=-1 ans=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;
    }

D XNOR Operation

E Trapezium

F We’re teapots

G Binary Operation

posted @ 2025-08-12 17:45  wzzkaifa  阅读(33)  评论(0)    收藏  举报