isbasov

I

Jan 24th, 2023
1,563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.13 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "container/heap"
  6.     "fmt"
  7.     "os"
  8.     //  "github.com/pkg/profile"
  9. )
  10.  
  11. // An Item is something we manage in a priority queue.
  12. type Item struct {
  13.     value    int // The value of the item; arbitrary.
  14.     priority int // The priority of the item in the queue.
  15.     // The index is needed by update and is maintained by the heap.Interface methods.
  16.     index int // The index of the item in the heap.
  17. }
  18.  
  19. type PriorityQueue []*Item
  20.  
  21. func (pq PriorityQueue) Len() int { return len(pq) }
  22.  
  23. func (pq PriorityQueue) Less(i, j int) bool {
  24.     // We want Pop to give us the highest, not lowest, priority so we use greater than here.
  25.     return pq[i].priority < pq[j].priority
  26.  
  27. }
  28.  
  29. func (pq PriorityQueue) Swap(i, j int) {
  30.     pq[i], pq[j] = pq[j], pq[i]
  31.     pq[i].index = i
  32.     pq[j].index = j
  33. }
  34.  
  35. func (pq *PriorityQueue) Push(x any) {
  36.     n := len(*pq)
  37.     item := x.(*Item)
  38.     item.index = n
  39.     *pq = append(*pq, item)
  40. }
  41.  
  42. func (pq *PriorityQueue) Pop() any {
  43.     old := *pq
  44.     n := len(old)
  45.     item := old[n-1]
  46.     old[n-1] = nil  // avoid memory leak
  47.     item.index = -1 // for safety
  48.     *pq = old[0 : n-1]
  49.     return item
  50. }
  51.  
  52. // update modifies the priority and value of an Item in the queue.
  53. func (pq *PriorityQueue) update(item *Item, value int, priority int) {
  54.     item.value = value
  55.     item.priority = priority
  56.     heap.Fix(pq, item.index)
  57. }
  58.  
  59. var t int
  60.  
  61. func main() {
  62.  
  63.     //  defer profile.Start().Stop()
  64.  
  65.     in := bufio.NewReader(os.Stdin)
  66.     var (
  67.         n, m, l, ld, a int
  68.     )
  69.  
  70.     fmt.Fscan(in, &n, &m)
  71.  
  72.     pq := make(PriorityQueue, n)
  73.     pq2 := make(PriorityQueue, 0)
  74.  
  75.     for i := 0; i < n; i++ {
  76.         fmt.Fscan(in, &a)
  77.  
  78.         pq[i] = &Item{
  79.             value:    a,
  80.             priority: a,
  81.             index:    i,
  82.         }
  83.     }
  84.  
  85.     heap.Init(&pq)
  86.     heap.Init(&pq2)
  87.     for j := 0; j < m; j++ {
  88.  
  89.         fmt.Fscan(in, &t, &l)
  90.  
  91.         for pq2.Len() > 0 {
  92.             it := heap.Pop(&pq2).(*Item)
  93.  
  94.             if it.priority <= t {
  95.                 it.priority = it.value
  96.                 heap.Push(&pq, it)
  97.             } else {
  98.                 heap.Push(&pq2, it)
  99.                 break
  100.             }
  101.         }
  102.  
  103.         if pq.Len() == 0 {
  104.             continue
  105.         }
  106.  
  107.         it := heap.Pop(&pq).(*Item)
  108.  
  109.         ld += it.value * l
  110.         it.priority = t + l
  111.         heap.Push(&pq2, it)
  112.  
  113.     }
  114.  
  115.     fmt.Printf("%d", ld)
  116.  
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment