結果

問題 No.393 2本の竹
ユーザー nCk_cvnCk_cv
提出日時 2016-07-12 01:57:45
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 5,106 bytes
コンパイル時間 2,291 ms
コンパイル使用メモリ 78,244 KB
実行使用メモリ 52,596 KB
最終ジャッジ日時 2024-04-23 01:47:01
合計ジャッジ時間 5,471 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
37,168 KB
testcase_01 RE -
testcase_02 AC 52 ms
37,028 KB
testcase_03 AC 54 ms
36,552 KB
testcase_04 AC 51 ms
36,964 KB
testcase_05 AC 52 ms
36,972 KB
testcase_06 WA -
testcase_07 AC 53 ms
37,168 KB
testcase_08 AC 56 ms
37,168 KB
testcase_09 AC 228 ms
52,596 KB
testcase_10 AC 68 ms
38,408 KB
testcase_11 AC 137 ms
46,172 KB
testcase_12 AC 171 ms
46,624 KB
testcase_13 AC 101 ms
41,352 KB
testcase_14 AC 91 ms
39,848 KB
testcase_15 AC 59 ms
36,988 KB
testcase_16 AC 51 ms
37,016 KB
testcase_17 AC 54 ms
36,992 KB
testcase_18 AC 52 ms
37,080 KB
testcase_19 RE -
testcase_20 AC 53 ms
36,952 KB
testcase_21 AC 55 ms
37,164 KB
testcase_22 AC 150 ms
46,080 KB
testcase_23 AC 99 ms
41,436 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 187 ms
50,664 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 = n1; 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