結果

問題 No.861 ケーキカット
ユーザー uwiuwi
提出日時 2019-08-09 23:42:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 688 ms / 1,000 ms
コード長 6,852 bytes
コンパイル時間 3,929 ms
コンパイル使用メモリ 91,600 KB
実行使用メモリ 115,508 KB
最終ジャッジ日時 2024-07-07 19:05:59
合計ジャッジ時間 15,901 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 269 ms
59,796 KB
testcase_01 AC 277 ms
60,644 KB
testcase_02 AC 321 ms
61,264 KB
testcase_03 AC 574 ms
103,764 KB
testcase_04 AC 278 ms
59,916 KB
testcase_05 AC 319 ms
60,160 KB
testcase_06 AC 688 ms
115,508 KB
testcase_07 AC 315 ms
61,396 KB
testcase_08 AC 293 ms
61,676 KB
testcase_09 AC 317 ms
61,628 KB
testcase_10 AC 302 ms
60,960 KB
testcase_11 AC 300 ms
60,164 KB
testcase_12 AC 639 ms
103,720 KB
testcase_13 AC 566 ms
104,296 KB
testcase_14 AC 583 ms
103,800 KB
testcase_15 AC 572 ms
105,288 KB
testcase_16 AC 592 ms
104,216 KB
testcase_17 AC 629 ms
104,036 KB
testcase_18 AC 563 ms
102,964 KB
testcase_19 AC 566 ms
104,864 KB
testcase_20 AC 631 ms
105,216 KB
testcase_21 AC 629 ms
104,352 KB
testcase_22 AC 570 ms
104,408 KB
testcase_23 AC 638 ms
105,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest190809;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;

public class E {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = 5, m = 5;
		int[][] a = new int[n][];
		for(int i = 0;i < n;i++){
			a[i] = na(m);
		}
		
		long[][] bells = bells(m);
		int B = (int)bells[m][0];
		List[][][] dp = new List[1<<m][B][2]; // 01,frontier,hidden
		for(int f = 0;f < 1<<m;f++){
			int[] q = new int[m];
			int p = 0;
			for(int i = 1;i < m;i++){
				if((f>>>i&1) != (f>>>i-1&1)){
					p++;
				}
				q[i] = p;
			}
			int code = encBell(q, bells);
			long s = 0;
			for(int i = 0;i < m;i++){
				s += f<<~i<0 ? a[0][i] : -a[0][i];
			}
			if(s >= 0){
				dp[f][code][0] = new ArrayList<Long>();
				dp[f][code][0].add(s);
			}
		}
		
		for(int i = 1;i < n;i++){
			for(int j = 0;j < m;j++){
				List[][][] ndp = new List[1<<m][B][2]; // 01,frontier,hidden
				for(int f = 0;f < 1<<m;f++){
					for(int t = 0;t < B;t++){
						int[] e = decBell(t, m, bells);
						for(int h = 0;h < 2;h++){
							if(dp[f][t][h] == null)continue;
							
							for(int nv = 0;nv < 2;nv++){
								int nf = f>>>1|nv<<m-1;
								int[] ne = newcell(e, m,
										(f&1) == nv,
										j > 0 && (f>>>m-1&1) == nv);
								
								int nh = h + newHidden(e, (f&1) == nv);
								
								if(nh < 2){
									int code = encBell(ne, bells);
									
									if(ndp[nf][code][nh] == null){
										ndp[nf][code][nh] = new ArrayList<Long>();
									}
//									tr(i, j, f, e, h, nf, ne, nh, dp[f][t][h]);
									
									for(long x : (ArrayList<Long>)dp[f][t][h]){
										ndp[nf][code][nh].add(x + (nv == 1 ? a[i][j] : -a[i][j]));
									}
								}
							}
						}
					}
				}
				
				dp = ndp;
			}
		}
		
		long min = Long.MAX_VALUE;
		for(int i = 0;i < 1<<m;i++){
			for(int j = 0;j < B;j++){
				for(int h = 0;h < 2;h++){
					if(dp[i][j][h] == null)continue;
					int[] e = decBell(j, m, bells);
					int max = 0;
					for(int v : e)max = Math.max(max, v);
					if(max+1 + h <= 2){
						for(long v : (ArrayList<Long>)dp[i][j][h]){
							min = Math.min(min, Math.abs(v));
						}
					}
				}
			}
		}
		out.println(min);
	}
	
	public static int newHidden(int[] a, boolean connectToUpper)
	{
		if(connectToUpper)return 0;
		if(a[0] != -1){
			for(int i = 1;i < a.length;i++){
				if(a[i] == 0)return 0;
			}
			return 1;
		}else{
			return 0;
		}
	}
	
	public static int[] newcell(int[] a, int m, boolean connectToUpper, boolean connectToLeft)
	{
		int n = a.length;
		int[] xa = new int[n];
		System.arraycopy(a, 1, xa, 0, n-1);
		xa[n-1] = n;
		if(connectToUpper){
			for(int i = 0;i < n-1;i++){
				if(xa[i] == a[n-m])xa[i] = n;
			}
		}
		if(connectToLeft){
			for(int i = 0;i < n-1;i++){
				if(xa[i] == a[n-1])xa[i] = n;
			}
		}
		return relocate(xa);
	}

	
	public static int[] relocate(int[] a)
	{
		int[] map = new int[12];
		Arrays.fill(map, -1);
		int p = 0;
		for(int i = 0;i < a.length;i++){
			if(a[i] == -1)continue;
			if(map[a[i]] == -1){
				map[a[i]] = p++;
			}
			a[i] = map[a[i]];
		}
		return a;
	}

	
	public static long[][] bells(int n)
	{
		long[][] bells = new long[n+1][];
		for(int i = 0;i <= n;i++)bells[i] = new long[n-i+1];
		Arrays.fill(bells[0], 1);
		for(int i = 1;i <= n;i++){
			for(int j = 0;j <= n-i;j++){
				bells[i][j] = bells[i-1][j] * j + bells[i-1][j+1];
			}
		}
		return bells;
	}
	
	// クラスタ分布から最小完全ハッシュ
	public static int encBell(int[] a, long[][] bells)
	{
		int max = 0;
		int ret = 0;
		for(int i = 0;i < a.length;i++){
			ret += bells[a.length-i-1][max+1] * a[i];
			if(a[i] > max)max = a[i];
		}
		return ret;
	}
	
	public static int[] decBell(long K, int m, long[][] bells)
	{
		if(K >= bells[m][0])return null;
		int[] a = new int[m];
		int max = 0;
		for(int i = 0;i < m;i++){
			a[i] = (int)(Math.min(max+1, K/bells[m-i-1][max+1]));
			K -= a[i] * bells[m-i-1][max+1];
			if(a[i] > max)max = a[i];
		}
		return a;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new E().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0