結果

問題 No.861 ケーキカット
ユーザー uwiuwi
提出日時 2019-08-09 22:20:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,584 bytes
コンパイル時間 3,978 ms
コンパイル使用メモリ 91,752 KB
実行使用メモリ 163,608 KB
最終ジャッジ日時 2024-07-19 12:18:12
合計ジャッジ時間 25,159 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 531 ms
81,104 KB
testcase_01 AC 538 ms
79,388 KB
testcase_02 AC 559 ms
80,388 KB
testcase_03 AC 949 ms
161,140 KB
testcase_04 AC 547 ms
78,892 KB
testcase_05 AC 551 ms
79,112 KB
testcase_06 AC 946 ms
151,836 KB
testcase_07 AC 558 ms
80,392 KB
testcase_08 AC 536 ms
79,832 KB
testcase_09 AC 573 ms
82,692 KB
testcase_10 AC 580 ms
82,632 KB
testcase_11 AC 539 ms
78,880 KB
testcase_12 WA -
testcase_13 AC 952 ms
159,124 KB
testcase_14 WA -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 897 ms
159,136 KB
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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[][] a = new int[5][];
		for(int i = 0;i < 5;i++){
			a[i] = na(5);
		}
		
		long[][] bells = bells(5);
		List[][][] dp = new List[1<<5][52][2]; // 01,frontier,hidden
		for(int f = 0;f < 1<<5;f++){
			int[] q = new int[5];
			int p = 0;
			for(int i = 1;i < 5;i++){
				if((f>>>i&1) != (f>>>i-1&1)){
					p++;
				}
				q[i] = p;
			}
			int code = encBell(q, bells);
			dp[f][code][0] = new ArrayList<Integer>();
			long s = 0;
			for(int i = 0;i < 5;i++){
				s += f<<~i<0 ? a[0][i] : -a[0][i];
			}
			dp[f][code][0].add(s);
		}
		
		for(int i = 1;i < 5;i++){
			for(int j = 0;j < 5;j++){
				List[][][] ndp = new List[1<<5][52][2]; // 01,frontier,hidden
				for(int f = 0;f < 1<<5;f++){
					for(int t = 0;t < 52;t++){
						int[] e = decBell(t, 5, 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)&31;
								
								int[] ne = new int[5];
								for(int k = 0;k < 4;k++)ne[k+1] = e[k];
								int nh = h;
								if((f>>>4&1) != nv){
									boolean ok = false;
									for(int k = 0;k < 4;k++){
										if(e[k] == e[4])ok = true;
									}
									if(!ok)nh++;
								}
								
								if(nh < 2){
									ne[0] = 10;
									if((f>>>4&1) == nv){
										for(int k = 1;k < 5;k++){
											if(ne[k] == e[4]){
												ne[k] = 10;
											}
										}
									}
									if(j > 0 && (f>>>0&1) == nv){
										for(int k = 1;k < 5;k++){
											if(ne[k] == e[0]){
												ne[k] = 10;
											}
										}
									}
									ne = relocate(ne);
									int code = encBell(ne, bells);
									
									if(ndp[nf][code][nh] == null){
										ndp[nf][code][nh] = new ArrayList<Long>();
									}
									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<<5;i++){
			for(int j = 0;j < 52;j++){
				for(int h = 0;h < 2;h++){
					if(dp[i][j][h] == null)continue;
					int[] e = decBell(j, 5, 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[] 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