zoukankan      html  css  js  c++  java
  • POJ 3253 优先队列 Fence Repair

    题目链接:http://poj.org/problem?id=3253

    分析:每次找到最小的两根接到一起,将其回,然后再选两个最小的接到一起,又放回....直到最后成了一根!!

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<iomanip>
    #include<queue>
    
    using namespace std;
    const int maxn=1000000;
    
    struct node {
        __int64 k;
        friend bool operator < (node x,node y) {
            return x.k>y.k;
        }
    } f[maxn],rt;
    priority_queue<node>M;
    int main() {
        int n; cin>>n;
        while(!M.empty())M.pop();
        for(int i=0; i<n; ++i) {
            cin>>f[i].k;
            M.push(f[i]);
        }
        __int64 sum=0;
        while(M.size()>1) {
            __int64 x,y;
            x=M.top().k; M.pop();
            y=M.top().k; M.pop();
            sum+=x+y;
            rt.k=x+y;
            M.push(rt);
        }
        cout<<sum<<endl;
        return 0;
    }
    


  • 相关阅读:
    HTML中Css补充资料
    HTML表单
    HTML盒子模型
    标准文档流
    什么使用面向对象
    static修饰
    static修饰
    列表样式
    java基础(9)
    java基础(8)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3057262.html
Copyright © 2011-2022 走看看