結果

問題 No.861 ケーキカット
ユーザー uwiuwi
提出日時 2019-08-09 23:42:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 535 ms / 1,000 ms
コード長 6,852 bytes
コンパイル時間 4,345 ms
コンパイル使用メモリ 84,132 KB
実行使用メモリ 123,508 KB
最終ジャッジ日時 2023-09-22 02:17:24
合計ジャッジ時間 16,463 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 268 ms
69,636 KB
testcase_01 AC 286 ms
69,764 KB
testcase_02 AC 294 ms
69,484 KB
testcase_03 AC 483 ms
112,524 KB
testcase_04 AC 291 ms
70,500 KB
testcase_05 AC 306 ms
70,452 KB
testcase_06 AC 512 ms
123,508 KB
testcase_07 AC 291 ms
71,148 KB
testcase_08 AC 298 ms
70,492 KB
testcase_09 AC 292 ms
70,548 KB
testcase_10 AC 296 ms
70,588 KB
testcase_11 AC 311 ms
70,472 KB
testcase_12 AC 485 ms
111,696 KB
testcase_13 AC 472 ms
112,000 KB
testcase_14 AC 482 ms
111,888 KB
testcase_15 AC 486 ms
112,972 KB
testcase_16 AC 482 ms
111,912 KB
testcase_17 AC 473 ms
112,396 KB
testcase_18 AC 484 ms
113,000 KB
testcase_19 AC 535 ms
111,924 KB
testcase_20 AC 476 ms
110,892 KB
testcase_21 AC 487 ms
113,176 KB
testcase_22 AC 483 ms
113,372 KB
testcase_23 AC 474 ms
111,624 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