よわよわ大学生の掃き溜め

自分の興味があることを備忘録も兼ねて記録するところ。

ABC196

2021/3/20に開催されたAtCoder Beginner Contest 196の考え方をメモるエントリです。

A - Difference Max

\displaystyle{
\begin{alignat}{5}
&a & &\leq & &x & &\leq & &b \\
&c & &\leq & &y & &\leq & &d
\end{alignat}
}

なので、

\displaystyle{
\begin{alignat}{5}
&a & &\leq & &x & &\leq & &b \\
-&d & &\leq & -&y & &\leq & -&c
\end{alignat}
}

であり、辺々足すと

\displaystyle{
a - d \leq x - y \leq b - c
}

となるので、\displaystyle{
x - y
}の最大値は\displaystyle{
b - c
}です。

B - Round Down

\displaystyle{
x
}の最大値が\displaystyle{
10^ {100}
}ととても大きいので、文字列で処理することを考え、

regex_replace(<元の文字列>, regex(<置換対象の正規表現>), <置換後の文字列>);

というメソッドがあるので、今回はこのようなコードになりました。

string s;
cin >> s;
s = regex_replace(s, regex("\\..*"), "");
cout << s << endl;

一つ目のバックスラッシュ\で二つ目のバックスラッシュ自体をエスケープしているので、最終的に\..*という正規表現を使っていることになります。

C - Doubled

\displaystyle{
x
}を十進法で表した時、その前半と後半が等しい\displaystyle{
x
}の個数を求めればよいので、「等しい前半と後半」に対して全探索を行いました。

ll n;
cin >> n;

int count = 0;
for (int i = 1; true; i++) {
  string s = to_string(i) + to_string(i);
  ll x = stoll(s);
  if (x <= n)
    count++;
  else
    break;
}
cout << count << endl;

例えば、forループの中でi=13のとき、xは1313となります。