結果

問題 No.393 2本の竹
ユーザー nCk_cvnCk_cv
提出日時 2016-07-12 02:01:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 202 ms / 1,000 ms
コード長 5,086 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 78,044 KB
実行使用メモリ 63,000 KB
最終ジャッジ日時 2024-04-25 18:12:27
合計ジャッジ時間 4,901 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
49,820 KB
testcase_01 AC 49 ms
50,168 KB
testcase_02 AC 49 ms
50,296 KB
testcase_03 AC 49 ms
50,040 KB
testcase_04 AC 49 ms
50,312 KB
testcase_05 AC 49 ms
50,196 KB
testcase_06 AC 47 ms
50,252 KB
testcase_07 AC 51 ms
49,936 KB
testcase_08 AC 51 ms
50,188 KB
testcase_09 AC 202 ms
63,000 KB
testcase_10 AC 65 ms
50,904 KB
testcase_11 AC 121 ms
57,384 KB
testcase_12 AC 149 ms
58,252 KB
testcase_13 AC 86 ms
52,572 KB
testcase_14 AC 65 ms
50,808 KB
testcase_15 AC 53 ms
50,264 KB
testcase_16 AC 50 ms
50,316 KB
testcase_17 AC 53 ms
50,412 KB
testcase_18 AC 50 ms
50,176 KB
testcase_19 AC 49 ms
50,244 KB
testcase_20 AC 49 ms
52,236 KB
testcase_21 AC 53 ms
49,840 KB
testcase_22 AC 137 ms
57,336 KB
testcase_23 AC 95 ms
55,320 KB
testcase_24 AC 49 ms
50,196 KB
testcase_25 AC 50 ms
50,064 KB
testcase_26 AC 50 ms
50,220 KB
testcase_27 AC 166 ms
62,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
	        }
	    }
	}
}
0