公式サイトはこちら。
1問目、2問目はおいといて、3問目。特に難しいアルゴリズムなど必要なかったのに頭が硬くてとけなくて悔しい・・・ときには冷静に見つめなおして書けるようになりたい。。
4問目は規則性見つけて短時間で解けました。3問目をあきらめるという選択も悪くはなかったのかな。今回、はやくも4回目のAtCoderでしたが初の500点問題正解です!やったー^^
こちらは2問目。
全体の長さと、節目にそった長さの2倍を足し算していき全体の長さを超えるかどうかを判断。
そのうえでどちらの節目で切るのかを距離で決めて、計算。というような感じです。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] len = new long[n];
long allLen = 0;
for (int i = 0; i < n; i++) {
len[i] = sc.nextLong();
allLen = allLen + len[i];
}
long before = 0;
long after = 0;
int num = 0;
while (after < allLen) {
before = after;
after = before + len[num] * 2;
num++;
}
long ans = Math.min(after - allLen, allLen - before);
System.out.println(ans);
}
}
こちらが4問目。
問題文にある操作を行うと
参加者の数字の各桁の和が変わらずに桁数が1減る
か
参加者の数字の各桁の和が9減って、桁数は変わらない
のどちらかになります。(9なのは、10→1になるから9減ります。)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
long sum = 0;
long digit = 0;
for (int i = 1; i <= m; i++) {
int d = sc.nextInt();
long c = sc.nextLong();
sum = sum + d * c;
digit = digit + c;
}
long ans = (sum - 1) / 9 + digit - 1;
System.out.println(ans);
}
}
規則さえ分かってしまえば結構シンプルですね。
最後にsum-1しているのは、9のときはOKなので1減らしてます。
明日もAtCoderBigginerContestあるけど、二日連続となると参加迷います、、
またこれからも精進していきます!