LoginSignup
1
0

More than 3 years have passed since last update.

【Java】AtCoderのABC-194に参加しました(レート:197→230)。

Last updated at Posted at 2021-03-06

こんばんは。

2021/3/6に、AtCoderのABC-194に参加しました。
レートは以下の通りとなっています。

image.png

時間ぎりぎりでC問題まで何とか解けました。
ただ、時間がかかったのと、C問題はトライ&エラーを繰り返したため低くなっています。
開き直ってもしょうがないですが、トライ&エラーは今の自分には必要なので、まあ良いかなと。順位は4443位でした。

レートは197→230に更新!よし200達成だ、おめ(^▽^)/!

A問題

問題文がややこしくて時間を取ってしまいました。
無脂乳固形分(A)と乳脂肪分(B)を入力。
乳固形分(A+B)、乳脂肪分(B)の割合に応じて、1~4の値を出力。

import java.util.Scanner;

public class Main{
public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  int a = sc.nextInt();
  int b = sc.nextInt();
  int nk = a+b;
  int ns = b;
  int flg = 0;
  //System.out.println(nk);
  if(nk>=15&&ns>=8){
    flg = 1;
  }else{
    if(nk>=10&&ns>=3){
    flg = 2;
    }else{
      if(nk>=3){
        flg=3;
      }else{
        flg=4;
      }
    }
  }
  System.out.println(flg);
}
}

B問題

N人の従業員、それぞれAの仕事時間とBの仕事時間がある。
両方片付けるのに最短の時間はいくらか。
(同じ従業員の場合はAi+Bi、違う従業員の場合は時間のかかる方)

import java.util.Scanner;

public class Main{
public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  int a[] = new int[n];
  int b[] = new int[n];
  for(int i=0;i<n;i++){
    a[i] = sc.nextInt();
    b[i] = sc.nextInt();
  }
  int min = 100001;
  int lg = 0;
  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        lg = 0;
        if(a[i]>b[j]){
          lg = a[i];
        }else{
          lg = b[j];
        }
        if(i==j){
          if((a[i]+b[j])<min){
            min = a[i]+b[j];
          }else{
          }
        }else{
          if(lg<min){
            min = lg;
          }else{
          }
        }
      }
  }


  System.out.println(min);
}
}

C問題

image.png

とのことです。
処理時間がかかるので、入力時に計算可能な値を計算しながら、ループをせずに合計値を求めるロジックに変えていきました。試行錯誤して時間ギリギリで何とかなりました。

import java.util.Scanner;

public class Main{
public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  long a[] = new long[n];
  long ttl =0;
  long ttl2 =0;
  long r2 = 0;
  long r2j = 0;
  for(int i=0;i<n;i++){
     a[i] = sc.nextLong();
     if(i>0){
     r2 = r2 + (-1)*a[i-1];
     r2j = r2j + a[i-1]*a[i-1];
     ttl2 = ttl2 + i*(a[i])*(a[i]) + (2)*a[i]*(r2) + r2j;
     }else{
     }
     //System.out.println(" i*(a[i])*(a[i]) " + i*(a[i])*(a[i]) + " r2 " + r2 + " r2j " + r2j + " ttl2 " + ttl2);
     // for(int j=0;j<i;j++){
     //   ttl = ttl + (a[i]-a[j])*(a[i]-a[j]);
     // }

  }
  //int total = 0;
  //for(int i=0;i<n;i++){
  //  for(int j=i+1;j<n;j++){
  //    total = total + (a[j]-a[i])*(a[j]-a[i]);
  //  }
  //}

  System.out.println(ttl2);
  //System.out.println(ttl);
  //System.out.println(total);
}
}

感想

今年以内に茶色を取得する目標ですが、意外と近づいてきた気がします。

プログラミングって書いているときは苦しく、終わると楽しいんですよね。
開発現場の気持ちを保っていく意味でも、競技プログラミングは良いかなと思いました。

これからもコツコツ取り組んで、茶色を目指したいと思います!
同じく灰色レベルで頑張っている方、お互い頑張りましょう!

1
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
1
0