結果

問題 No.168 ものさし
ユーザー AreTrashAreTrash
提出日時 2016-09-26 06:08:14
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 731 ms / 2,000 ms
コード長 6,197 bytes
コンパイル時間 4,924 ms
コンパイル使用メモリ 114,400 KB
実行使用メモリ 77,620 KB
最終ジャッジ日時 2023-08-25 21:03:33
合計ジャッジ時間 9,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
35,144 KB
testcase_01 AC 66 ms
21,772 KB
testcase_02 AC 67 ms
23,924 KB
testcase_03 AC 66 ms
23,856 KB
testcase_04 AC 65 ms
23,940 KB
testcase_05 AC 65 ms
21,708 KB
testcase_06 AC 66 ms
21,808 KB
testcase_07 AC 65 ms
21,664 KB
testcase_08 AC 65 ms
21,772 KB
testcase_09 AC 67 ms
21,892 KB
testcase_10 AC 72 ms
21,824 KB
testcase_11 AC 157 ms
30,996 KB
testcase_12 AC 366 ms
65,204 KB
testcase_13 AC 489 ms
75,144 KB
testcase_14 AC 539 ms
72,888 KB
testcase_15 AC 68 ms
21,784 KB
testcase_16 AC 69 ms
21,684 KB
testcase_17 AC 71 ms
21,920 KB
testcase_18 AC 83 ms
25,916 KB
testcase_19 AC 613 ms
69,868 KB
testcase_20 AC 731 ms
77,620 KB
testcase_21 AC 528 ms
75,180 KB
testcase_22 AC 713 ms
75,768 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace No168_2{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var N = sr.Next<int>();
            var P = sr.Next<long>(N, 2).Select(a => new{X = a[0], Y = a[1]}).ToArray();

            Func<long, long, long, long, long> squareDist = (x1, y1, x2, y2) => (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);

            var dj = new Dijkstra_Fixed(N);
            for(var i = 0; i < N; i++){
                for(var j = i + 1; j < N; j++){
                    var c = squareDist(P[i].X, P[i].Y, P[j].X, P[j].Y);
                    dj.AddEdge(i, j, c);
                    dj.AddEdge(j, i, c);
                }
            }

            var res = dj.Solve(0)[N - 1];
            Func<int, int> check = x => 100L * x * x >= res ? 2 : 0;
            Console.WriteLine(~Ex.BinarySearch(check, 1, 0, 150000000) * 10);
            //---------------------------------
        }
    }

    public class Ex{
        //発見した場合は         f(ret) == key
        //発見できなかった場合は f(ret - 1) < ~key < f(ret)
        public static int BinarySearch(Func<int, int> f, int key, int left, int right){
            while(left != right){
                var mid = (left + right) / 2;
                if(f(mid) == key) return mid;
                if(f(mid) > key) right = mid;
                if(f(mid) < key) left = mid + 1;
            }
            return ~left;
        } 
    }

    public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>{
        private readonly List<T> _list = new List<T>();
        public int Count => _list.Count;

        IEnumerator<T> IEnumerable<T>.GetEnumerator(){
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator(){
            return _list.GetEnumerator();
        }

        public void Clear(){
            _list.Clear();
        }

        public bool Contains(T value){
            return _list.Contains(value);
        }

        public T Peek(){
            return _list[0];
        }

        public void Enqueue(T value){
            _list.Add(value);
            PushHeap();
        }

        public T Dequeue(){
            var ret = _list[0];
            _list[0] = _list[Count - 1];
            _list.RemoveAt(Count - 1);
            PopHeap();
            return ret;
        }

        private void PushHeap(){
            var i = Count - 1;
            while(i != 0){
                var p = (i - 1) / 2;
                if(_list[i].CompareTo(_list[p]) > 0) return;

                SwapIndex(i, i = p);
            }
        }

        private void PopHeap(){
            var i = 0;
            while(true){
                var l = 2 * i + 1;
                var r = l + 1;

                var maxi = i;
                if(l < Count && _list[maxi].CompareTo(_list[l]) > 0) maxi = l;
                if(r < Count && _list[maxi].CompareTo(_list[r]) > 0) maxi = r;
                if(maxi == i) return;

                SwapIndex(i, i = maxi);
            }
        }

        private void SwapIndex(int left, int right){
            var tmp = _list[left];
            _list[left] = _list[right];
            _list[right] = tmp;
        }
    }

    public class Dijkstra_Fixed{
        public class Edge{
            public int To;
            public long Cost;
        }

        public class Node : IComparable<Node>{
            public int Id;
            public long Cost;

            public int CompareTo(Node node){
                return Math.Sign(Cost - node.Cost);
            }
        }

        private readonly List<Edge>[] _adj;

        public Dijkstra_Fixed(int v){
            _adj = new List<Edge>[v];
            for(var i = 0; i < v; i++) _adj[i] = new List<Edge>();
        }

        public void AddEdge(int from, int to, long cost){
            _adj[from].Add(new Edge{To = to, Cost = cost});
        }

        public long[] Solve(int start){
            var v = _adj.Length;
            var res = new long[v];

            for(var i = 0; i < v; i++) res[i] = long.MaxValue;
            res[start] = 0;

            var pq = new PriorityQueue<Node>();
            pq.Enqueue(new Node{Id = start, Cost = 0});

            while(pq.Count > 0){
                var node = pq.Dequeue();
                var from = node.Id;

                foreach(var edge in _adj[from]){
                    var to = edge.To;
                    var cost = Math.Max(node.Cost, edge.Cost);

                    if(cost >= res[to]) continue;
                    res[to] = cost;
                    pq.Enqueue(new Node{Id = to, Cost = cost});
                }
            }
            return res;
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}
0