結果

問題 No.5020 Averaging
ユーザー uwiuwi
提出日時 2024-03-03 11:23:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 979 ms / 1,000 ms
コード長 6,943 bytes
コンパイル時間 12,539 ms
コンパイル使用メモリ 92,432 KB
実行使用メモリ 58,832 KB
スコア 16,794,277
最終ジャッジ日時 2024-03-03 11:25:07
合計ジャッジ時間 57,486 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 978 ms
58,692 KB
testcase_01 AC 971 ms
58,332 KB
testcase_02 AC 961 ms
58,460 KB
testcase_03 AC 966 ms
58,456 KB
testcase_04 AC 969 ms
58,456 KB
testcase_05 AC 963 ms
58,452 KB
testcase_06 AC 967 ms
58,452 KB
testcase_07 AC 963 ms
58,448 KB
testcase_08 AC 959 ms
58,220 KB
testcase_09 AC 967 ms
58,412 KB
testcase_10 AC 964 ms
58,448 KB
testcase_11 AC 964 ms
58,324 KB
testcase_12 AC 959 ms
58,452 KB
testcase_13 AC 978 ms
58,360 KB
testcase_14 AC 958 ms
58,448 KB
testcase_15 AC 961 ms
58,452 KB
testcase_16 AC 970 ms
58,460 KB
testcase_17 AC 964 ms
58,428 KB
testcase_18 AC 963 ms
58,448 KB
testcase_19 AC 962 ms
58,356 KB
testcase_20 AC 965 ms
58,444 KB
testcase_21 AC 962 ms
58,428 KB
testcase_22 AC 970 ms
58,448 KB
testcase_23 AC 964 ms
58,452 KB
testcase_24 AC 960 ms
58,452 KB
testcase_25 AC 960 ms
58,480 KB
testcase_26 AC 979 ms
58,832 KB
testcase_27 AC 966 ms
58,692 KB
testcase_28 AC 962 ms
58,452 KB
testcase_29 AC 965 ms
58,244 KB
testcase_30 AC 973 ms
58,452 KB
testcase_31 AC 965 ms
58,700 KB
testcase_32 AC 963 ms
58,692 KB
testcase_33 AC 962 ms
58,320 KB
testcase_34 AC 974 ms
58,452 KB
testcase_35 AC 965 ms
58,460 KB
testcase_36 AC 964 ms
58,452 KB
testcase_37 AC 961 ms
58,448 KB
testcase_38 AC 976 ms
58,440 KB
testcase_39 AC 960 ms
58,232 KB
testcase_40 AC 966 ms
58,460 KB
testcase_41 AC 968 ms
58,324 KB
testcase_42 AC 969 ms
58,328 KB
testcase_43 AC 965 ms
58,488 KB
testcase_44 AC 968 ms
56,892 KB
testcase_45 AC 973 ms
58,700 KB
testcase_46 AC 964 ms
58,452 KB
testcase_47 AC 960 ms
58,448 KB
testcase_48 AC 961 ms
56,504 KB
testcase_49 AC 960 ms
58,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package extra;

import java.io.PrintWriter;
import java.util.*;
import java.util.function.*;

public class N5020AN {
	static Scanner in;
	static PrintWriter out;
	static String INPUT = "";
	
	static void solve()
	{
		int n = ni();
		ab = new long[n][];
		for(int i = 0;i < n;i++){
			ab[i] = new long[]{nl(), nl()};
		}
		final long O = 500000000000000000L;

		final int m = 50;
//		ToLongBiFunction<int[], Integer> score = (route, turn) -> {
//			long x = (ab[0][0]-O)/(1L<<m), y = (ab[0][1]-O)/(1L<<m);
//			for(int i = 0;i < turn;i++){
//				x += (ab[route[i]+1][0]-O)/(1L<<(i+1));
//				y += (ab[route[i]+1][1]-O)/(1L<<(i+1));
//			}
//			return O - Math.max(Math.abs(x), Math.abs(y));
//		};
		ToLongFunction<int[]> score = route -> {
			long x = ab[0][0], y = ab[0][1];
			for(int r : route){
				x = (x + ab[r+1][0]) / 2;
				y = (y + ab[r+1][1]) / 2;
			}
			return O - Math.max(Math.abs(x-O), Math.abs(y-O));
		};
		Random gen = new Random();
//		Result res = bogo(score, () -> {
//			int[] route = new int[m];
//			for(int i = 0;i < m;i++){
//				route[i] = gen.nextInt(44)+1;
//			}
//			return route;
//		}, 800);
		Result res = doOnepointAnnealing(score, 44, m, new int[m], 800L, new SplittableRandom(),
				1e17, 0.999
				);
//		tr(O - res.score);
		out.println(m);
		for(int r : res.route){
//			out.println(1 + " " + (r+1));
			out.println(1 + " " + (r+2));
		}
	}

	static long[][] ab;

	static class Result
	{
		int[] route;
		long score;

		Result(int[] route, long score)
		{
			this.route = route;
			this.score = score;
		}
	}

	public static Result doBeamSearch(ToLongFunction<int[]> score, Predicate<int[]> isValid, int limval, int len, int W)
	{
		PriorityQueue<Result> beam = new PriorityQueue<>(Comparator.comparingLong(x -> x.score));
		beam.add(new Result(new int[0], score.applyAsLong(new int[0])));

		for(int i = 0;i < len;i++){
			PriorityQueue<Result> nbeam = new PriorityQueue<>(Comparator.comparingLong(x -> x.score));
			for(Result d : beam){
				for(int k = 0;k < limval;k++){
					int[] nroute = Arrays.copyOf(d.route, d.route.length+1);
					nroute[nroute.length-1] = k;
					if(isValid.test(nroute)){
						long s = score.applyAsLong(nroute);
						if(!nbeam.isEmpty() && s <= nbeam.peek().score)continue;
						nbeam.add(new Result(nroute, s));
						if(nbeam.size() > W)nbeam.poll();
					}
				}
			}
			beam = nbeam;
		}
		while(beam.size() > 1)beam.poll();
		Result last = beam.poll();
		assert last != null;
		return new Result(last.route, last.score);
	}

	public static Result doOnepointAnnealing(ToLongFunction<int[]> score, int limval, int len, int[] initial, long tl, SplittableRandom gen, double iniT, double M)
	{
		long S = System.currentTimeMillis();
		assert initial.length == len;
		int[] cur = Arrays.copyOf(initial, len);
		long curscore = score.applyAsLong(cur);
		int[] argbest = Arrays.copyOf(cur, len);
		long best = curscore;

		double T = iniT;
		int rep = 0;
		while(System.currentTimeMillis() - S < tl){
			int ind = gen.nextInt(len);
			int val = gen.nextInt(limval);
			int bak = cur[ind];
			cur[ind] = val;
			if(val == bak)continue;
			long nexscore = score.applyAsLong(cur);
			if(nexscore >= curscore || gen.nextDouble() < Math.exp((nexscore - curscore) / T)){
				curscore = nexscore;
				if(curscore > best){
					best = curscore;
					argbest = Arrays.copyOf(cur, len);
				}
			}else{
				cur[ind] = bak;
			}
			T *= M;
			rep++;
		}
		return new Result(argbest, best);
	}

	public static Result doOnepointHillClimbing(ToLongFunction<int[]> score, int limval, int len, int[] initial, long tl, SplittableRandom gen)
	{
		long S = System.currentTimeMillis();
		assert initial.length == len;
		int[] cur = Arrays.copyOf(initial, len);
		long curscore = score.applyAsLong(cur);

		int rep = 0;
		int updated = 0;
		while(System.currentTimeMillis() - S < tl){
			int ind = gen.nextInt(len);
			int val = gen.nextInt(limval);
			int bak = cur[ind];
			cur[ind] = val;
			if(val == bak)continue;
			long nexscore = score.applyAsLong(cur);
			if(nexscore >= curscore){
				curscore = nexscore;
				updated++;
			}else{
				cur[ind] = bak;
			}
			rep++;
		}
//		tr(rep, updated);
		return new Result(cur, curscore);
	}

	/**
	 * greedyの判定用スコアに、そこからさらにgreedyした場合の最終スコアを採用する
	 * O((limval*len)^2*score)
	 * @param score
	 * @param isValid
	 * @param limval
	 * @param len
	 * @return
	 */
	public static Result richGreedy(ToLongBiFunction<int[], Integer> score, BiPredicate<int[], Integer> isValid, int limval, int len)
	{
		int[] route = new int[len];
		for(int i = 1;i <= len;i++){
			long max = Long.MIN_VALUE;
			int arg = -1;
			for(int v = 0;v < limval;v++){
				route[i-1] = v;
				if(isValid.test(route, i)){
					Result res = greedy(score, isValid, limval, len, i, route);
					if(res.score > max){
						max = res.score;
						arg = v;
					}
				}
			}
			route[i-1] = arg;
		}
		long s = score.applyAsLong(route, len);
		return new Result(route, s);
	}

	public static Result greedy(ToLongBiFunction<int[], Integer> score, BiPredicate<int[], Integer> isValid, int limval, int len)
	{
		int[] route = new int[len];
		return greedy(score, isValid, limval, len, 0, route);
	}

	/**
	 * 各フェーズでスコアを最大化するものを採用
	 * O(limval*len*score)
	 * @param score
	 * @param isValid
	 * @param limval
	 * @param len
	 * @param start
	 * @return
	 */
	public static Result greedy(ToLongBiFunction<int[], Integer> score, BiPredicate<int[], Integer> isValid, int limval, int len, int start, int[] route)
	{
		for(int i = start + 1;i <= len;i++){
			long max = Long.MIN_VALUE;
			int arg = -1;
			for(int v = 0;v < limval;v++){
				route[i-1] = v;
				if(isValid.test(route, i)){
					long ls = score.applyAsLong(route, i);
					if(ls > max){
						max = ls;
						arg = v;
					}
				}
			}
			route[i-1] = arg;
		}
		long s = score.applyAsLong(route, len);
		return new Result(route, s);
	}

	/**
	 * 時間まで列をランダム生成
	 * @param score
	 * @param supplier
	 * @param tl
	 * @return
	 */
	public static Result bogo(ToLongFunction<int[]> score, Supplier<int[]> supplier, long tl)
	{
		long S = System.currentTimeMillis();

		long best = Long.MIN_VALUE;
		int[] arg = null;
		while(System.currentTimeMillis() - S < tl){
			int[] route = supplier.get();
			long s = score.applyAsLong(route);
			if(s > best){
				best = s;
				arg = route;
			}
		}

		return new Result(arg, best);
	}

	public static void main(String[] args) throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
	}
	
	static int ni() { return Integer.parseInt(in.next()); }
	static long nl() { return Long.parseLong(in.next()); }
	static double nd() { return Double.parseDouble(in.next()); }
	static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
0