0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[c++] ABC048 #AtCoder

Posted at

概要

問題の解き方をまとめました。

  • 2025.10.6 A-D

A AtCoder *** Contest #ABC048-A

問題

「 AtCoder $s$ Contest 」の頭文字をとって略称を出力する。

  • $1 \leq |s| \leq 100$
  • $s$ の一文字目は大文字
  • $s$ の二文字目以降は英小文字

解法

  1. それぞれの頭文字を出力する。
サンプルコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)

vector<string> S(3);

int main() {
  rep(i, 3) cin >> S.at(i);
  
  cout << 'A' << S.at(1).at(0) << 'C' << endl;
  return 0;
}

B Between a and b ... #ABC048-B

問題

$a$ 以上 $b$ 以下の整数のうち $x$ で割り切れるものの個数を求める。

  • $0 \leq a \leq b \leq 10^{18}$
  • $1 \leq x \leq 10^{18}$

解法

  1. $a = 0$ の時には、$0$ も $x$ で割り切れるから、$b / x$ に $1$ を足す。
  2. それ以外の場合は、$x$ で割り切れる $b$ までの整数の数から、$x$ で割り切れる $a - 1$ までの整数の数を引く。
サンプルコード
#include <bits/stdc++.h>
using namespace std;

int64_t a, b, x;

int main() {
  cin >> a >> b >> x;
  
  if (a == 0) cout << (b / x) + 1 << endl;
  else cout << (b / x) - ((a-1) / x) << endl;
  return 0;
}

C Boxes and Candies #ABC048-C

問題

$N$ 個の箱に $a_i$ 個のキャンディが入っている。どの隣り合う 2 つの箱を見ても、それらの箱に入っているキャンディの個数の総和を $x$ 以下にするためには、何個のキャンディを食べれば良いかを求める。

  • $2 \leq N \leq 10^5$
  • $0 \leq a_i \leq 10^9$
  • $0 \leq x \leq 10^9$

解法

  1. 前から箱を見ていき、隣り合う箱に入っているキャンディの個数の総和が $x$ を超えている場合、後ろの箱の方からキャンディの個数を引いていく。後ろの箱のキャンディを全部食べても $x$ を超える場合は、前の箱のキャンディを食べる。
サンプルコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)

int N, x;
vector<int> A;

int main() {
  cin >> N >> x;
  A.resize(N); rep(i, N) cin >> A.at(i);
  
  int64_t ans = 0; //食べたキャンディの個数
  rep(i, N-1) { //前の箱から順に見ていく
    int sum = A.at(i) + A.at(i+1); //隣り合う箱のキャンディの個数の総和
    if (sum > x) {
      int t = sum - x; ans += t; //食べる必要があるキャンディの個数
      if (t <= A.at(i+1)) A.at(i+1) -= t; //後ろの箱からキャンディを食べる
      else { A.at(i) -= t - A.at(i+1); A.at(i+1) = 0; } //後ろの箱のキャンディで足りない場合は、前の箱のキャンディも食べる
    }
  }
  cout << ans << endl;
  return 0;
}

D An Ordinary Game #ABC048-D

問題

長さ 3 以上の文字列 $s$ がある。両端以外の文字を取り除く操作をしていく。文字を取り除いたときに同じ文字が隣り合う場合は、操作を行えない。2 人が交互に操作を行っていき、先に操作を行えなくなった人が負ける。どちらが勝つかを判定する。

  • $3 \leq |s| \leq 10^5$
  • $s$ は英小文字のみからなる
  • $s$ の中に同一の文字が隣り合う箇所はない

解法

  1. 両端が同じ文字だった場合は、最後に長さ 3 の文字列が残る。
  2. 両端が違う文字だった場合は、最後に長さ 2 の文字列が残る。
  3. 取り除く文字が奇数個だった場合は先手が勝ち、偶数個だった場合は後手が勝つ。
サンプルコード
#include <bits/stdc++.h>
using namespace std;

string s;

int main() {
  cin >> s;
  
  int count = s.size();
  if (s.front() == s.back()) count -= 3;
  else count -= 2;
  
  cout << (count % 2 ? "First" : "Second") << endl;
  return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?