結果

問題 No.5020 Averaging
ユーザー uwiuwi
提出日時 2024-02-25 15:46:51
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 108 ms / 1,000 ms
コード長 5,162 bytes
コンパイル時間 8,905 ms
コンパイル使用メモリ 158,240 KB
実行使用メモリ 183,212 KB
スコア 21,917,887
最終ジャッジ日時 2024-02-25 15:47:08
合計ジャッジ時間 15,184 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
34,852 KB
testcase_01 AC 84 ms
34,468 KB
testcase_02 AC 85 ms
34,468 KB
testcase_03 AC 86 ms
34,468 KB
testcase_04 AC 87 ms
34,724 KB
testcase_05 AC 86 ms
34,340 KB
testcase_06 AC 85 ms
34,468 KB
testcase_07 AC 83 ms
34,340 KB
testcase_08 AC 84 ms
34,724 KB
testcase_09 AC 88 ms
34,724 KB
testcase_10 AC 85 ms
34,852 KB
testcase_11 AC 84 ms
34,724 KB
testcase_12 AC 83 ms
34,724 KB
testcase_13 AC 83 ms
34,468 KB
testcase_14 AC 83 ms
34,468 KB
testcase_15 AC 83 ms
34,852 KB
testcase_16 AC 84 ms
34,852 KB
testcase_17 AC 84 ms
34,468 KB
testcase_18 AC 84 ms
34,852 KB
testcase_19 AC 86 ms
34,980 KB
testcase_20 AC 86 ms
34,980 KB
testcase_21 AC 88 ms
34,468 KB
testcase_22 AC 83 ms
34,468 KB
testcase_23 AC 86 ms
35,108 KB
testcase_24 AC 82 ms
34,468 KB
testcase_25 AC 84 ms
34,468 KB
testcase_26 AC 82 ms
34,468 KB
testcase_27 AC 83 ms
34,724 KB
testcase_28 AC 82 ms
34,468 KB
testcase_29 AC 82 ms
34,596 KB
testcase_30 AC 82 ms
34,596 KB
testcase_31 AC 83 ms
34,852 KB
testcase_32 AC 84 ms
34,596 KB
testcase_33 AC 108 ms
34,468 KB
testcase_34 AC 83 ms
34,596 KB
testcase_35 AC 84 ms
34,852 KB
testcase_36 AC 82 ms
34,340 KB
testcase_37 AC 84 ms
34,724 KB
testcase_38 AC 82 ms
34,340 KB
testcase_39 AC 84 ms
34,852 KB
testcase_40 AC 84 ms
34,852 KB
testcase_41 AC 82 ms
34,596 KB
testcase_42 AC 83 ms
34,852 KB
testcase_43 AC 84 ms
34,980 KB
testcase_44 AC 101 ms
34,468 KB
testcase_45 AC 83 ms
34,468 KB
testcase_46 AC 85 ms
34,852 KB
testcase_47 AC 82 ms
34,596 KB
testcase_48 AC 85 ms
34,468 KB
testcase_49 AC 83 ms
183,212 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (94 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using System.Reflection;

// TODO: 1次元で考える
// 3個あれば表現可能そうだけど
// a < b < c
// これだとどれかの2つが同じになって単調になりそう
// a,b,c
// a,(b+c)/2
// (1/2,1/4,1/4) (0,1/2,1/2)
// (1/2,1/4,1/4) (1/4,3/8,3/8)
class Program
{
    static void Main()
    {
        int n = ni();
        long[][] co = nml(n);

        long O = 500000000000000000L;

        List<int[]> ret = new List<int[]>();
        long lmin = O*2;
        int argmin = -1;
        for(int i = 0;i < n;i++){
            long x = (co[0][0] + co[i][0]) / 2;
            long y = (co[0][1] + co[i][1]) / 2;
            long d = Math.Abs(x - O) + Math.Abs(y - O);
            if(d < lmin){
                lmin = d;
                argmin = i;
            }
        }
        if(argmin > 0){
            long x = (co[0][0] + co[argmin][0]) / 2;
            long y = (co[0][1] + co[argmin][1]) / 2;
            co[0][0] = co[argmin][0] = x;
            co[0][1] = co[argmin][1] = y;
            ret.Add(new int[]{0, argmin});
        }

        long tx = O*2 - co[0][0];
        long ty = O*2 - co[0][1];

        long[][] sco = co.OrderBy(c => c[0]).ToArray();

        int p = 0, q = n-1;
        for(int i = 0;i < 4;i++){
            List<long[]> ls = new List<long[]>();
            for(int j = 0;j < 2;j++){
                if(sco[p][2] == 0)p++;
                ls.Add(sco[p++]);
            }
            for(int j = 0;j < 2;j++){
                if(sco[q][2] == 0)q--;
                ls.Add(sco[q--]);
            }
            double[] xs = new double[]{
                Convert.ToDouble(ls[0][0]) - tx, 
                Convert.ToDouble(ls[1][0]) - tx, 
                Convert.ToDouble(ls[2][0]) - tx, 
                Convert.ToDouble(ls[3][0]) - tx
            };

            min = Double.PositiveInfinity;
            simulate(xs, 10, new List<int>());
            foreach(int ind in best){
                long x = (ls[ind][0] + ls[ind+1][0]) / 2;
                long y = (ls[ind][1] + ls[ind+1][1]) / 2;
                ret.Add(new int[]{
                    Convert.ToInt32(ls[ind][2]),
                    Convert.ToInt32(ls[ind+1][2])
                });
                ls[ind][0] = ls[ind+1][0] = x;
                ls[ind][1] = ls[ind+1][1] = y;
            }
        }

        long[][] ysco = sco.OrderBy(c => Math.Abs(c[0] - tx)).ToArray();
        List<long[]> ls2 = new List<long[]>();
        for(int i = 0;i < n;i++){
            if(ysco[i][2] == 0)continue;
            ls2.Add(ysco[i]);
            if(ls2.Count == 4)break;
        }

        long[][] sls = ls2.OrderBy(c => c[1]).ToArray();
        double[] ys = new double[]{
            Convert.ToDouble(ls2[0][1]) - ty, 
            Convert.ToDouble(ls2[1][1]) - ty, 
            Convert.ToDouble(ls2[2][1]) - ty, 
            Convert.ToDouble(ls2[3][1]) - ty
        };

        min = Double.PositiveInfinity;
        simulate(ys, int.Min(14, 49 - ret.Count), new List<int>());
        foreach(int ind in best){
            long x = (ls2[ind][0] + ls2[ind+1][0]) / 2;
            long y = (ls2[ind][1] + ls2[ind+1][1]) / 2;
            ret.Add(new int[]{
                Convert.ToInt32(ls2[ind][2]),
                Convert.ToInt32(ls2[ind+1][2])
            });
            ls2[ind][0] = ls2[ind+1][0] = x;
            ls2[ind][1] = ls2[ind+1][1] = y;
        }
        
        int arg = -1;
        double vald = Double.PositiveInfinity;
        for(int i = 0;i < n;i++){
            if(co[i][2] == 0)continue;
            double d = Math.Max(Math.Abs(co[i][0] - tx), Math.Abs(co[i][1] - ty));
            if(d < vald){
                vald = d;
                arg = i;
            }
        }
        ret.Add(new int[]{0, (int)co[arg][2]});

        Console.WriteLine(ret.Count);
        foreach(int[] r in ret){
            Console.WriteLine($"{r[0]+1} {r[1]+1}");
        }
    }

    static double min;
    static List<int> best = new List<int>();

    static void simulate(double[] a, int rem, List<int> route)
    {
        for(int i = 0;i < 4;i++){
            if(Math.Abs(a[i]) < min){
                min = Math.Abs(a[i]);
                best = new List<int>(route);
            }            
        }
        if(rem == 0)return;
        for(int i = 0;i < 3;i++){
            if(a[i] == a[i+1])continue;
            double[] b = (double[])a.Clone();
            double v = (b[i] + b[i+1]) / 2;
            b[i] = b[i+1] = v;
            route.Add(i);
            simulate(b, rem-1, route);
            route.RemoveAt(route.Count-1);
        }
    }

    static string ns()
    {
        return Console.ReadLine();
    }

    static int ni()
    {
        return Convert.ToInt32(ns());
    }

    static int[] na()
    {
        return ns().Split(' ').Select(int.Parse).ToArray();
    }

    static long[] nal()
    {
        return ns().Split(' ').Select(long.Parse).ToArray();
    }

    static long[][] nml(int n)
    {
        long[][] ret = new long[n][];
        for (int i = 0; i < n; i++)
        {
            long[] f = nal();
            ret[i] = new long[]{f[0], f[1], i};
        }
        return ret;
    }
}
0