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?

More than 1 year has passed since last update.

ABC064 の問題を解いてみました

Last updated at Posted at 2024-05-05

A問題

3桁の数字は、$100r + 10g + b$ と表せます。

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

signed main(){
  int r, g, b;
  cin >> r >> g >> b;
  
  int num = r * 100 + g * 10 + b;
  
  if(num % 4 == 0){
    cout << "YES" << endl;
  }
  else{
    cout << "NO" << endl;
  }
}

B問題

端から端まで順番にプレゼントを配っていくと、最短距離になります。

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

signed main(){
  int n;
  cin >> n;
  vector<int> a(n);
  
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
  
  sort(a.begin(),a.end()); //並び替える
  
  cout << a[n - 1] - a[0] << endl;
  //橋から橋の距離を出力
}

C問題

レートが3200以上の人が大きく絡んできます。
色の種類を最大にしたい場合は、存在しない色に3200以上のレートの人を割り振るべきです。
色の種類を最小にしたい場合は、もうすでにある色に割り振るべきです。
(3200以上のレートの人は、黒色なども選べることに注意)

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

signed main(){
  int n;
  cin >> n;
  vector<int> a(n), color(8, 0);
  //colorはそれぞれの色が何人いるか記録する配列
  bool ok = false;
  int freedom = 0;
  
  for(int i = 0; i < n; i++){
    cin >> a[i];
    if(a[i] < 3200){
      ok = true;
      //3200以下の人がいない場合への対応
      
      color[a[i] / 400]++;
      //色に下から1,2,...と数字を割り振っておく
      //その色の人の人数を記録する
    }
    else{
      freedom++; //色を自由に選べる人の数を記録しておく
    }
  }
  
  if(ok){
    int kind = 0; //何種類の色の人がいるか数える変数
    for(int i = 0; i < 8; i++){
      if(color[i]) kind++;
    }
    cout << kind << endl;
    
    cout << kind + freedom << endl;
  }
  else{
    cout << 1 << endl;
    cout << n << endl;
  }
}

D問題

まず、最初に対応する '(' がない ')' の数を数えて、 '(' を文字列の先頭に挿入します。

そして、 対応する ')' がない '(' の数を数えて、 ')' を文字列の末尾に挿入します。

そうしてできた文字列を出力すればよいです。

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

int main(void){
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0, ans = 0;
  
  for(int i = 0; i < s.size(); i++){
    if(s[i] == ')'){
      cnt++; //加える必要がある ( の数を数えておく
      
      ans = max(ans, cnt); //最大値を記録しておく
      // 例 )()(
    }
    else cnt--;
  }
  
  for(int i = 0; i < ans; i++) s = '(' + s;
  // ) に対応する ( を s の最初に挿入する
  
  cnt = 0, ans = 0;

  for(int i = 0; i < s.size(); i++){
    if(s[i] == '(') ans++; // 加える必要がある ) の数を数えておく
    else ans--;
  }
  
  for(int i = 0; i < ans; i++) s += ')';
  // ( に対応する ) を s の末尾に挿入する
  
  cout << s << 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?