AtCoder Beginner Contest 235をやった。
記事投稿サボってました。自己満投稿ですが、これからもよろしくお願いします。
C問題はTLEになったので参考にしないでください。
※もし、Javaで解かれた方いましたら、コメントいただきたく!!
A
3つの数字の並び替え問題
詳細は例題にある通り。直感的に3回並び替えて足し算する方法が楽かなと思ったのでそれでやりました。
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
String abc = scan.nextLine();
int sum = 0;
for(int i = 0; i < 3; i++){
sum += Integer.parseInt(abc);
abc = abc.concat(abc.substring(0, 1));
abc = abc.substring(1, 4);
}
System.out.println(sum);
}
}
B
高さを求める問題、日本語にするのは難しいので詳細は問題を確認してください
制約が最大でも10^5ループとあったので、単純なforループで行けると思い決行
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int[] Hi = new int[N];
int ans = 0;
for(int i = 0; i < N; i++){
Hi[i] = scan.nextInt();
if(i >= 1){
if(Hi[i-1] < Hi[i]){
ans = Hi[i];
} else {
ans = Hi[i-1];
break;
}
}
}
System.out.println(ans);
}
}
C
上にも書きましたが、参考にはしないでください。TLEがでます。
PHPなどにある連想配列ベースでやればたぶんできるんだろうなーとは思いましたがJavaのMapの使い方に挫折。。。
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
long N = scan.nextLong();
long Q = scan.nextLong();
long[] aIndex = new long[(int) N];
for(int i = 0; i < N; i++){
aIndex[i] = scan.nextLong();
}
for(int i = 0; i < Q; i++){
int cnt = 0;
long xNum = scan.nextLong();
long kNum = scan.nextLong();
for(int j = 0; j < aIndex.length; j++){
if(aIndex[j] == xNum){
cnt++;
if(cnt == kNum){
System.out.println(j+1);
break;
}
}
}
if(cnt < kNum){
System.out.println(-1);
}
}
}
}