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?

ABC046 ARC062 の問題を解いてみました

Last updated at Posted at 2023-12-29

A問題

$a$ と $b$ と $c$ が同じペンキなら 1 を
$a , b , c$ の 2つが同じペンキなら 2 を
3つの色がすべて異なるなら 3 を 出力します。

#include <bits/stdc++.h>
using namespace std;
//Created by karaju.

int main(void){
  int a, b, c;
  cin >> a >> b >> c;
  
  if(a == b && b == c) cout << 1 << endl;
  else if(a == b || b == c || c == a) cout << 2 << endl;
  else cout << 3 << endl;
  
}
setを使った別解
#include <bits/stdc++.h>
using namespace std;
//Created by karaju.

int main(void){
  set<int> paint;
  for(int i = 0; i < 3; i++){
    int a;
    cin >> a;
    paint.insert(a);
  }
  cout << paint.size() << endl;
}

B問題

一番左のボールは $K$ 通り、 それ以外は左のボールの色を除いて、 $K - 1$ 通りの塗り方があります。
合計で何通りかを繰り返しで計算しましょう。(オーバーフローに注意)

#include <bits/stdc++.h>
using namespace std;
//Created by karaju.

int main(void){
  int n, k;
  cin >> n >> k;
  long long ans = k; 
  for(int i = 1; i < n; i++){
    ans *= k - 1;
  }
  cout << ans << endl;
}

C問題

全探索では、 この提出 のように、TLEになってしまいます。
なので、全探索ではない解き方を考える必要があります。

まず、前回確認したときよりも得票数が減ることはありえません。
そして、高橋くんの得票数が$T_i$の倍数であり、青木くんの得票数が$A_i$の倍数である必要があります。

なので、
$前回の高橋くんの得票数 \leq n \times T_i, 前回の青木くんの得票数 \leq n \times A_i$
を満たす最小の自然数$n$を求められれば、今回確認したときの2人の得票数を求めることができます。
この$n$は以下の式で求められます。
$n = max(\lceil \frac{前回の高橋くんの得票数}{T_i}\rceil,\lceil \frac{前回の青木くんの得票数}{A_i}\rceil) $

$n$の値が求められたので、2人の得票数も求められます。
この処理を繰り返せば、この問題は解くことができます。

#include <bits/stdc++.h>
using namespace std;
//Created by karaju.

int main(void){
  int n;
  cin >> n;
  long long t_now = 1, a_now = 1, t, a;
  // t_now, a_now は 両者の得票数
  for(int i = 0; i < n; i++){
    cin >> t >> a;
    
    long long mag = max( (t_now + t - 1) / t, (a_now + a - 1) / a );
    // a / b の切り上げは、 (a + b - 1) / b で 計算できる
    
    t_now = t * mag;
    a_now = a * mag;
  }
  cout << a_now + t_now << endl;
}

D問題

問題を読んで考えてみると、できるだけパーの回数が多いほうが良いということがわかります。そして、勝つ方法は、 相手がグーを出したときに自分がパーを出す という方法のみです。
なので、相手がパーを出したらパーで対応して、相手がグーを出したらできるだけパーで対応するというのが最善手です。

このことから、答えは $自分が出せるパーの回数 - 相手のパーの回数$ で求められます。

#include <bits/stdc++.h>
using namespace std;
//Created by karaju.

int main(void){
  string s;
  cin >> s;
  int g = 0, p = 0;
  //グー・パーの数をカウントする変数
  for(int i = 0; i < s.size(); i++){
    if(s[i] == 'g') g++;
    else p++; //パーの回数を数える
  }
  int times = s.size() / 2;
  //自分の出せるパーの回数を求める
  
  cout << times - p << endl;
}
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?