結果
| 問題 |
No.393 2本の竹
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-14 14:39:18 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 344 ms / 1,000 ms |
| コード長 | 5,575 bytes |
| コンパイル時間 | 3,854 ms |
| コンパイル使用メモリ | 77,924 KB |
| 実行使用メモリ | 121,856 KB |
| 最終ジャッジ日時 | 2024-11-17 05:56:44 |
| 合計ジャッジ時間 | 7,493 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class TwoBamboo {
/* メモ化用Map */
private static int[][] calculatedArray;
public static void main(String[] args) {
FastScanner cin = new FastScanner(System.in);
int d = cin.nextInt();
int[][][] allQuestions = new int[d][2][];
for (int i = 0; i < d; i++) {
/* 2本の竹情報 */
allQuestions[i][0] = new int[2];
allQuestions[i][0][0] = cin.nextInt();
allQuestions[i][0][1] = cin.nextInt();
/* 要求数 */
int requestsNum = cin.nextInt();
/* 要求情報 */
allQuestions[i][1] = new int[requestsNum];
for (int j = 0; j < requestsNum; j++) {
allQuestions[i][1][j] = cin.nextInt();
}
}
cin.close();
for (int[][] question : allQuestions) {
System.out.println(answer(question[0], question[1]));
}
}
/* 各データセット単位で解く */
private static int answer(int[] bamboo, int[] requests) {
Arrays.sort(requests);
Arrays.sort(bamboo);
calculatedArray = new int[requests.length][bamboo[0] + 1];
return getNum(0, bamboo[0], bamboo[1], requests);
}
private static int getNum(int currentIndex, int shortBamboo, int longBamboo, int[] requests) {
/* 要求されるのは長さのみで重みはないので、要求を小さい順にソートしたとき、R(n)の要求に応えずにR(n+1)の要求に応えたほうがよい、というケースはない。なので、必ず昇順ソートされた要求に対し、どちらかの竹から切リ出して次の要求を処理する */
if (calculatedArray[currentIndex][shortBamboo] != 0) {
/* 計算済み */
return calculatedArray[currentIndex][shortBamboo];
}
int shortResult = 0;
if (requests[currentIndex] <= shortBamboo) {
shortResult++;
if (currentIndex + 1 < requests.length) {
shortResult += getNum(currentIndex + 1, shortBamboo - requests[currentIndex], longBamboo, requests);
}
}
int longResult = 0;
if (requests[currentIndex] <= longBamboo) {
longResult++;
if (currentIndex + 1 < requests.length) {
longResult += getNum(currentIndex + 1, shortBamboo, longBamboo - requests[currentIndex], requests);
}
}
int result = shortResult > longResult ? shortResult : longResult;
calculatedArray[currentIndex][shortBamboo] = result;
return result;
}
static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public FastScanner(InputStream in) {
this.in = in;
}
public void close() {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}