Advertisement
strCarne

Untitled

Apr 20th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.84 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "sort"
  6. )
  7.  
  8. func binSearch(arr []int, target int) int {
  9.     low, high := 0, len(arr)-1
  10.     for low <= high {
  11.         mid := (low + high) / 2
  12.         if arr[mid] == target && mid+1 < len(arr) && arr[mid+1] != target {
  13.             return mid
  14.         } else if arr[mid] > target {
  15.             high = mid - 1
  16.         } else {
  17.             low = mid + 1
  18.         }
  19.     }
  20.     return -2
  21. }
  22.  
  23. func main() {
  24.     var n int
  25.     fmt.Scan(&n)
  26.     for i := 0; i < n; i++ {
  27.         var size int
  28.         fmt.Scan(&size)
  29.         arr := make([]int, size)
  30.         for j := range arr {
  31.             fmt.Scan(&arr[j])
  32.         }
  33.         sort.Ints(arr)
  34.         sum := 0
  35.  
  36.         start := 0
  37.         end := binSearch(arr, arr[start]) + 1
  38.         for end != -1 {
  39.             sum += arr[start] * (end - start - (end-start)/3)
  40.             start = end
  41.             end = binSearch(arr, arr[start]) + 1
  42.         }
  43.         sum += arr[start] * (len(arr) - start - (len(arr)-start)/3)
  44.         fmt.Println(sum)
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement