結果
| 問題 |
No.5 数字のブロック
|
| ユーザー |
|
| 提出日時 | 2016-09-03 16:46:54 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 422 ms / 5,000 ms |
| コード長 | 1,501 bytes |
| コンパイル時間 | 3,654 ms |
| コンパイル使用メモリ | 77,184 KB |
| 実行使用メモリ | 58,948 KB |
| 最終ジャッジ日時 | 2024-11-18 09:24:18 |
| 合計ジャッジ時間 | 11,785 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 34 |
ソースコード
import java.util.Scanner;
public class Run {
public static void main (String arg[]) {
Scanner scan = new Scanner(System.in);
int L = scan.nextInt();
int N = scan.nextInt();
if (L < 1||L > 10000||N < 1||N > 10000) System.exit(1);
int[] W = new int[N];
for (int i = 0; i < N; i++) {
W[i] = scan.nextInt();
if (W[i] < 1||W[i] > L) System.exit(1);
}
for (int i = N; i > 1; i--) {
heapSort(i, W);
swap(W, 0, i-1);
}
int blocks = 0;
for (int i = 0; i < N; i++) {
L -= W[i];
if (L < 0) break;
blocks++;
}
System.out.println(blocks);
}
public static void heapSort (int leaf, int[] A) {
int root = leaf/2-1;
int tmp;
for (int i = root; i >= 0; i--) {
while ((root+1)*2 <= leaf) {
if ((root+1)*2 == leaf) {
tmp = (root+1)*2 - 1;
} else {
if (A[(root+1)*2 - 1] < A[(root+1)*2]) tmp = (root+1)*2;
else tmp = (root+1)*2 - 1;
}
if (A[root] < A[tmp]) {
swap(A, root, tmp);
root = tmp;
} else break;
}
root = i - 1;
}
}
public static void swap (int[] A, int a, int b){
int tmp;
tmp = A[a];
A[a] = A[b];
A[b] = tmp;
}
}