douxiaochun4964 2019-02-03 20:37
浏览 56

如何缩短执行时间

I was writing this code for "competitive Programming". It consisted of only 1 loop but gave "Time Limit Exceed" if n = 100000. Can Go be considered for competitive Programming?

fmt.Scanln(&n, &k, &m)
for i := 0; i < n; i++ {
    fmt.Scanf("%d", &z)
    if m >= 0 {
        if z > x {
            x = z
            m--
        }
        if i == n-1 {
            m++
        }
    } else {
        if cnt == 0 {
            x = 0
        }
        x += z
        cnt++
    }
}
if m == 0 {
    f = float64(x / (n - m))
}
if k < m {
    x += k
} else {
    x += m
}
f = float64(x)
fmt.Println(f)

"codeforces.com/problemset/problem/1111/B -- Average Superhero Gang Power"

  • 写回答

1条回答 默认 最新

  • dqwnxdhb88531 2019-02-04 18:23
    关注

    Go execution time can be improved to milliseconds for n = 100000, under the 1 second time limit.


    For example, a complete solution to the problem,

    // Problem - 1111B - Codeforces
    // B. Average Superhero Gang Power
    // http://codeforces.com/problemset/problem/1111/B
    
    package main
    
    import (
        "bufio"
        "fmt"
        "io"
        "os"
        "sort"
        "strconv"
    )
    
    func readInt(s *bufio.Scanner) (int, error) {
        if s.Scan() {
            i, err := strconv.Atoi(s.Text())
            if err != nil {
                return 0, err
            }
            return i, nil
        }
        err := s.Err()
        if err == nil {
            err = io.EOF
        }
        return 0, err
    }
    
    func main() {
        s := bufio.NewScanner(os.Stdin)
        s.Split(bufio.ScanWords)
    
        var n, k, m int
        var err error
        n, err = readInt(s)
        if err != nil {
            panic(err)
        }
        if n < 1 || n > 10e5 {
            panic("invalid n")
        }
        k, err = readInt(s)
        if err != nil {
            panic(err)
        }
        if k < 1 || k > 10e5 {
            panic("invalid k")
        }
        m, err = readInt(s)
        if err != nil {
            panic(err)
        }
        if m < 1 || m > 10e7 {
            panic("invalid m")
        }
    
        a := make([]int, n)
        sum := int64(0)
        for i := 0; i < n; i++ {
            ai, err := readInt(s)
            if err != nil {
                panic(err)
            }
            if ai < 1 || ai > 10e6 {
                panic("invalid a")
            }
            a[i] = ai
            sum += int64(ai)
        }
        sort.Slice(a, func(i, j int) bool {
            return a[i] > a[j]
        })
    
        i, ik := 0, k
        for ; m > 0; m-- {
            j := len(a) - 1
            if j >= 1 && (int64(n-1)*(1+sum) < int64(n)*(sum-int64(a[j]))) {
                // delete
                sum -= int64(a[j])
                a = a[:j]
            } else {
                // increment
                sum++
                a[i]++
                ik--
                if ik <= 0 {
                    ik = k
                    i++
                    if i >= len(a) {
                        break
                    }
                }
            }
        }
    
        // fmt.Println(sum, len(a))
        avg := float64(sum) / float64(len(a))
    
        fmt.Printf("%.20f
    ", avg)
    }
    

    Output:

    $ go run heroes.go
    2 4 6
    4 7
    11.00000000000000000000
    $ 
    
    $ go run heroes.go
    4 2 6
    1 3 2 3
    5.00000000000000000000
    $ 
    

    A benchmark for a cast of avengers with random powers, where n := 100000, k := 100, and m := 100000:

    $ ls -s -h heroes.test
    676K heroes.test
    $ go build heroes.go
    $ time ./heroes < heroes.test
    708329.74652807507663965225
    real    0m0.067s
    user    0m0.038s
    sys     0m0.004s
    $ 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用