AtCoder ABC 024 A&B&C
2019/05/27
問題名修正
C問題のコードの書き方を修正 int[][]の生成部分
A - 動物園
- とりあえず合算した後に割引対象なら割引すればよい
private void solveA() {
int[] first = IntStream.range(0, 4).map(i -> nextInt()).toArray();
int[] second = IntStream.range(0, 2).map(i -> nextInt()).toArray();
int child = second[0] * first[0];
int adult = second[1] * first[1];
int total = child + adult;
int sum = Arrays.stream(second).sum();
if (sum >= first[3]) {
total -= (sum * first[2]);
}
out.println(total);
}
B - 自動ドア
-
処理をそのまま書いたのを載せておく
- 簡略化したのをコメントアウトしている
-
内容は
- 現在の時刻でドアは何秒開くか?
- 現在の時刻は
- 前の開の時刻+T秒経過後
- もう閉じているのでT秒追加で開く
- 前の開の時刻+T秒経過前
- 閉じる前に人が通った
- (前の開の時刻+T秒)- 現在の時刻 を計算して、余計にカウントしている秒数をマイナス
- 今の時刻からT秒追加で開く
- 閉じる前に人が通った
- 前の開の時刻+T秒経過後
- 現在の時刻は
- 現在の時刻でドアは何秒開くか?
-
解法の説明がいまいち取れない。。。
private void solveB() {
int numN = nextInt();
int numT = nextInt();
int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();
long res = 0;
long preTime = 0;
for (int i = 0; i < wk.length; i++) {
long openTime = 0;
long currentTime = wk[i];
if (i == 0) {
preTime = currentTime;
openTime = numT;
} else {
if (preTime + numT > currentTime) {
openTime -= (preTime + numT) - currentTime;
openTime += numT;
preTime = currentTime;
} else {
preTime = currentTime;
openTime = numT;
}
}
// if (i != 0 && wk[i - 1] + numT > currentTime) {
// openTime -= (wk[i - 1] + numT) - currentTime;
// openTime += numT;
// } else {
// openTime = numT;
// }
res += openTime;
}
out.println(res);
}
C - 民族大移動
- 常にその日に一番いける街まで行ってしまったほうが良い
- スタートできる日を決めて(移動可能範囲内に入っている一番最初の日)
- その日に行ける街まで行く(大きい街へ行くのか小さい街へ行くのかはその部族次第)
- 次の日のスタートは現在いる街
- 上記を、小さい番号の街から大きい番号の街へ移動するパターンと、大きい番号→小さい番号のパターンを作る
private void solveC() {
int n = nextInt();
int d = nextInt();
int k = nextInt();
int[][] aLR = Stream.generate(() -> new int[] { nextInt(), nextInt() }).limit(d).toArray(int[][]::new);
// int[][] aLR = IntStream.range(0, d).collect(() -> new int[d][2],
// (t, i) -> {
// t[i][0] = nextInt();
// t[i][1] = nextInt();
// }, (t, u) -> {
// Stream.concat(Arrays.stream(t), Arrays.stream(u));
// });
int[][] aST = Stream.generate(() -> new int[] { nextInt(), nextInt() }).limit(k).toArray(int[][]::new);
// int[][] aST = IntStream.range(0, k).collect(() -> new int[k][2],
// (t, i) -> {
// t[i][0] = nextInt();
// t[i][1] = nextInt();
// }, (t, u) -> {
// Stream.concat(Arrays.stream(t), Arrays.stream(u));
// });
for (int[] js : aST) {
long day = getDay(aLR, js[0], js[1]);
out.println(day);
}
}
private long getDay(int[][] aLR, int start, int end) {
int current = start;
if (start < end) {
for (int i = 0; i < aLR.length; i++) {
if (aLR[i][0] <= current && current <= aLR[i][1]) {
current = aLR[i][1];
}
if (current >= end) {
return i + 1;
}
}
} else {
for (int i = 0; i < aLR.length; i++) {
if (aLR[i][0] <= current && current <= aLR[i][1]) {
current = aLR[i][0];
}
if (current <= end) {
return i + 1;
}
}
}
return -1;
}