zoukankan      html  css  js  c++  java
  • SDUT2423 Fence Repair(哈夫曼树)

    题目链接

    分析:

    原来是哈夫曼,想错了。比赛的时候以为水题呢。

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXN 20010
    
    int a[MAXN];
    
    int cmp(const void *a, const void *b){
        return (*(int *)a) - (*(int *)b);
    }
    
    int main(){
        int n, i, j, t;
        long long ans=0;
        scanf("%d", &n);
        for(i=0; i<n; i++){
            scanf("%d", &a[i]);
        }
        qsort(a, n, sizeof(a[0]), cmp);
        for(i=1; i<n; i++){
            t = (a[i]+a[i-1]);
            ans += t;
            for(j=i+1; j<n; j++){
                if(t > a[j]) a[j-1] = a[j];
                else break;
            }
            a[j-1] = t;
        }
        printf("%lld\n", ans);
        return 0;
    }

    使用优先队列:

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    int main(){
        priority_queue< int, vector<int>, greater<int> > q;
        int n, i, t, a, b;
        long long ans = 0, item;
        cin >> n;
        for(i=0; i<n; i++){
            cin >> t;
            q.push(t);
        }
        while(q.size() > 1){
            a = q.top(); q.pop();
            b = q.top(); q.pop();
            ans += (item = a+b);
            q.push(item);
        }
        cout << ans << endl;
    
        return 0;
    }
  • 相关阅读:
    jQuery基础及选择器
    JavaScript面向对象
    JavaScript操作DOM
    JavaScript Bom对象
    jquery内容
    jQuery基础
    正则表达式
    表单校验
    使用jQuery操作DOM
    jQuery中的动画
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2965606.html
Copyright © 2011-2022 走看看