Submission #17334240


Source Code Expand

import Foundation
extension Sequence {
    func permutations() -> [[Element]] {
        func _permutations<T>(of values: [T], indices: Range<Int>, result: inout [[T]]) {
            if indices.isEmpty {
                result.append(values)
                return
            }
            var values = values
            for i in indices {
                values.swapAt(indices.lowerBound, i)
                _permutations(of: values, indices: (indices.lowerBound + 1) ..< indices.upperBound, result: &result)
            }
        }
 
        var result: [[Element]] = []
        let values = Array(self)
        _permutations(of: values, indices: values.indices, result: &result)
        return result
    }
}
 
// 2乗する関数
func pow2(n:Int)->Int{
    return n*n
}
 
// 標準入力からnを受け取る
let n = Int(readLine()!)!
 
// 標準入力からx,yを受け取る
// x,yを持っておく配列mを用意
var m:[[Int]] = []
for _ in 0..<n{
 
    // mに入力値を入れていく
    let xy = readLine()!.split(separator:" ").map{Int($0)!}
    m.append(xy)
}
 
// 順列の生成
var check = m.permutations()
 
// 答えを入れる変数を用意
var ans:Double = 0.0
 
// 順列全探索
for i in check{
 
    // iの要素について全探索
    for j in 0..<n-1{
    
        // i[j]とi[j+1]の距離をansに追加
        ans += sqrt(Double( pow2(n:i[j][0] - i[j+1][0]) + pow2(n:i[j][1] - i[j+1][1])))
    }
}
 
// n!で割って平均をとる
for i in 1...n{
    ans/=Double(i)
}
 
// ansの出力
print(ans)

Submission Info

Submission Time
Task C - Average Length
User tardigrade
Language Swift (5.2.1)
Score 300
Code Size 1602 Byte
Status AC
Exec Time 79 ms
Memory 18192 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 15
Set Name Test Cases
Sample sample00, sample01, sample02
All handmade03, handmade04, handmade05, random06, random07, random08, random09, random10, random11, random12, random13, random14, sample00, sample01, sample02
Case Name Status Exec Time Memory
handmade03 AC 79 ms 17780 KiB
handmade04 AC 28 ms 17924 KiB
handmade05 AC 30 ms 18024 KiB
random06 AC 12 ms 13376 KiB
random07 AC 9 ms 13224 KiB
random08 AC 8 ms 13316 KiB
random09 AC 10 ms 13564 KiB
random10 AC 12 ms 13400 KiB
random11 AC 14 ms 13784 KiB
random12 AC 32 ms 18040 KiB
random13 AC 31 ms 18044 KiB
random14 AC 26 ms 18192 KiB
sample00 AC 8 ms 13312 KiB
sample01 AC 16 ms 13156 KiB
sample02 AC 27 ms 17864 KiB