結果
| 問題 |
No.393 2本の竹
|
| コンテスト | |
| ユーザー |
nCk_cv
|
| 提出日時 | 2016-07-12 02:01:45 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 185 ms / 1,000 ms |
| コード長 | 5,086 bytes |
| コンパイル時間 | 2,311 ms |
| コンパイル使用メモリ | 78,024 KB |
| 実行使用メモリ | 52,016 KB |
| 最終ジャッジ日時 | 2024-11-08 05:38:30 |
| 合計ジャッジ時間 | 5,484 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;
class Main {
static int[] vy = {1,0,-1,0};
static int[] vx = {0,1,0,-1};
static long mod = 1000000007;
static HashMap<String,Boolean> map = new HashMap<String,Boolean>();
public static void main(String[] args) {
FastScanner sc = new FastScanner();
//Scanner sc = new Scanner(System.in);
int d = sc.nextInt();
for(int i = 0; i < d; i++) {
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int m = sc.nextInt();
int[] A = new int[m];
for(int j = 0; j < m; j++) {
A[j] = sc.nextInt();
}
Arrays.sort(A);
int sumN1 = n1;
int sumN2 = n2;
int id = 0;
while(true) {
if(id == m) break;
if(sumN1 >= A[id]) {
sumN1 -= A[id];
id++;
continue;
}
else if(sumN2 >= A[id]) {
sumN2 -= A[id];
id++;
continue;
}
else {
//うまく切り詰めて入る可能性があるかどうかチェック
if(sumN1 + sumN2 < A[id]) break;
//もっとも空き空間が大きくなる切り詰め方探し
//PTA Aの方をギリギリまで詰めてBを空ける
boolean[][] dp = new boolean[id+1][n1 + 1];
dp[0][0] = true;
for(int j = 0; j < id; j++) {
for(int k = 0; k < n1+1; k++) {
if(!dp[j][k]) continue;
if(k + A[j] <= n1) dp[j + 1][k + A[j]] = true;
dp[j + 1][k] = true;
}
}
int max = 0;
for(int j = n1; j >= 0; j--) {
if(dp[id][j]) {
max = j;
break;
}
}
int openSize = n2 - ((n1 - sumN1) + (n2 - sumN2) - max);
if(openSize >= A[id]) {
sumN1 = n1 - max;
sumN2 = openSize - A[id];
id++;
continue;
}
dp = new boolean[id+1][n2 + 1];
dp[0][0] = true;
for(int j = 0; j < id; j++) {
for(int k = 0; k < n2+1; k++) {
if(!dp[j][k]) continue;
if(k + A[j] <= n2) dp[j + 1][k + A[j]] = true;
dp[j + 1][k] = true;
}
}
max = 0;
for(int j = n2; j >= 0; j--) {
if(dp[id][j]) {
max = j;
break;
}
}
openSize = n1 - ((n1 - sumN1) + (n2 - sumN2) - max);
if(openSize >= A[id]) {
sumN1 = openSize - A[id];
sumN2 = n2 - max;
id++;
continue;
}
break;
//PTB Bの方をギリギリまで詰めてAを空ける
}
}
System.out.println(id);
}
}
static class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
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;}
private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
public boolean hasNext() { skipUnprintable(); 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() {
if (!hasNext()) throw new NoSuchElementException();
int 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();
}
}
}
}
nCk_cv