結果

問題 No.393 2本の竹
ユーザー nCk_cvnCk_cv
提出日時 2016-07-12 02:01:45
言語 Java21
(openjdk 21)
結果
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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
36,616 KB
testcase_01 AC 51 ms
36,580 KB
testcase_02 AC 51 ms
36,812 KB
testcase_03 AC 51 ms
36,376 KB
testcase_04 AC 50 ms
36,568 KB
testcase_05 AC 52 ms
36,372 KB
testcase_06 AC 50 ms
36,528 KB
testcase_07 AC 53 ms
36,772 KB
testcase_08 AC 53 ms
36,704 KB
testcase_09 AC 184 ms
52,016 KB
testcase_10 AC 71 ms
38,100 KB
testcase_11 AC 136 ms
45,896 KB
testcase_12 AC 167 ms
46,276 KB
testcase_13 AC 97 ms
41,144 KB
testcase_14 AC 84 ms
39,176 KB
testcase_15 AC 58 ms
37,080 KB
testcase_16 AC 50 ms
36,372 KB
testcase_17 AC 54 ms
36,780 KB
testcase_18 AC 54 ms
36,952 KB
testcase_19 AC 53 ms
36,560 KB
testcase_20 AC 53 ms
36,752 KB
testcase_21 AC 58 ms
37,024 KB
testcase_22 AC 146 ms
46,116 KB
testcase_23 AC 101 ms
41,280 KB
testcase_24 AC 56 ms
37,032 KB
testcase_25 AC 57 ms
37,188 KB
testcase_26 AC 53 ms
36,744 KB
testcase_27 AC 185 ms
50,120 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