首页 > 试题广场 >

牛牛算数

[编程题]牛牛算数
  • 热度指数:1976 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
牛牛现在在学习计算机,他想通过计算机计算n个数的和。

但是计算机计算数字的和是有花费的,比如计算x,y两个数的和,需要花费秒。
计算机一次只能计算一次,牛牛想知道自己怎么合理安排计算的顺序,可以使得花费的时间最短。

输出计算n个数字和的最小花费的时间。


示例1

输入

5,76,[81,30,76,24,84]

输出

48944
示例2

输入

5,70,[1,2,3,3,4]

输出

2030

备注:

给定a数组,ai表示第i个数的大小。

 ,  

class Solution {
public:
    /**
     * 返回一个数字表示输出计算n个数字和的最小花费的时间。
     * @param n int整型 表示有n个数。
     * @param c int整型 参数c
     * @param a int整型vector ai表示第i个数的大小
     * @return long长整型
     */
    long long solve(int n, int c, vector<int>& a) {
        long long s = 0, x, y;
        priority_queue<long long, vector<long long>, greater<long long>> q;
        for(int i=0;i<a.size();i++)
            q.push(a[i]);
        for(int i=1;i<a.size();i++){
            x = q.top();
            q.pop();
            y = q.top();
            q.pop();
            s += x+y;
            q.push(x+y);
        }
        return s*c;
    }
};

发表于 2020-06-26 10:11:25 回复(0)
// 有时候 掌握一个优秀的数据结构也是很重要的
    public long solve (int n, int c, int[] a) {
        // write code here
        PriorityQueue<Integer> heap = new PriorityQueue<>(); // 小顶堆
        
        for(int i = 0; i < a.length; i++) {
            heap.offer(a[i]);
        }
        
        long cost = 0;
        
        while(heap.size() > 1) {
            int num1 = heap.poll();
            int num2 = heap.poll();
            int temp = num1 + num2;
            cost += c*num1 + c*num2;
            heap.offer(temp);
        }
        
        return cost;
    }

发表于 2021-05-12 12:48:55 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 表示有n个数。
     * @param c int整型 参数c
     * @param a int整型一维数组 ai表示第i个数的大小
     * @return long长整型
     */
    int index;
    public long solve (int n, int c, int[] a) {
        //优先队列+比较器
        int pre = 0;
        int cur = 0;
        long sum = 0;
        index = 0;
        int heap[] = new int[n];
        for(int i = 0; i < a.length; i++) {
            heapInsert(heap, a[i]);
        }
        while(index > 0) {
            if(index == 1) {//不包含
                return sum * c;
            }
            pre = heap[0];
            heaplify(heap);
            cur = heap[0];
            heaplify(heap);
            sum += pre + cur;
            heapInsert(heap, pre + cur);
        }
        return sum;//走不到
    }
    public void heapInsert(int[] heap, int value) {
        int cur = index;
        heap[index++] = value;
        while(cur > 0) {
            int parent = (cur - 1)/2;
            if(heap[parent] > heap[cur] ) {
                int temp = heap[parent];
                heap[parent] = heap[cur];
                heap[cur] = temp;
                cur = parent;
            }else {
                break;
            }
        }
    }
    public void heaplify(int[] heap) {
        index--;
        heap[0] = heap[index];
        int cur = 0;
        while(cur < index) {
            int leftChild = cur * 2 + 1;
            int rightChild = cur * 2 + 2;
            int smallest = cur;
            if(leftChild < index && heap[leftChild] < heap[cur]) {
                smallest = leftChild;
            }
            if(rightChild < index && heap[rightChild] < heap[smallest]) {
                smallest = rightChild;
            }
            if(smallest == cur) {
                break;
            }else {
                int temp = heap[smallest];
                heap[smallest] = heap[cur];
                heap[cur] = temp;
                cur = smallest;
            }
        }
    }
}

发表于 2021-07-03 21:02:49 回复(0)
优先队列priorityqueue,初始化,存值进去,拿出来删除相加计算,存进去;反复操作,优先队列中数个数小于2跳出循环,返回值
基本就这样,别的不会
发表于 2020-07-21 17:24:41 回复(0)
1、要用PriorityQueue;
2、结果为long,因此中间存储变量也要弄Long,不然通不过。
发表于 2020-06-08 23:26:59 回复(0)
简单的哈夫曼树,用小根堆来实现
class Solution {
public:
    /**
     * 返回一个数字表示输出计算n个数字和的最小花费的时间。
     * @param n int整型 表示有n个数。
     * @param c int整型 参数c
     * @param a int整型vector ai表示第i个数的大小
     * @return long长整型
     */
    // min heap
    
    long long solve(int n, int c, vector<int>& a) {
        // write code here
        long long res = 0;
        priority_queue<long long, vector<long long>, greater<long long> > heap;
        for(int x : a){
            heap.push(x);
        }
        long long t1, t2;
        for(int i = 1; i < a.size(); ++i){
            t1 = heap.top();
            heap.pop();
            t2 = heap.top();
            heap.pop();
            res += t1 + t2;
            heap.push(t1 + t2);
        }
        return res * c;
    }
};


发表于 2020-05-19 13:09:51 回复(0)

问题信息

难度:
6条回答 4172浏览

热门推荐

通过挑战的用户

查看代码