【POJ】[3253]Fence Repair

这里写图片描述

之前好像做过一个与这类似的
不过好像比这简单

#include<stdio.h>
#include<queue>
using namespace std;
struct node {
    int x;
//  bool friend operator<(node A,node B) {
//      A.x<B.x;
//  }以前一直用的这种写法突然不对了…… 
    const bool operator<(node a) const {
        return a.x<x;
    }
};
int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    int n;
    while(scanf("%d",&n)!=EOF) {
        priority_queue<node>q;
        for(int i=0; i<n; i++) {
            node T;
            scanf("%d",&T.x);
            q.push(T);
        }
        long long sum=0;
        for(int i=1; i<n; i++) {
            node T,t1,t2;
            t1=q.top();
            q.pop();
            t2=q.top();
            q.pop();
            T.x=t1.x+t2.x;
            sum+=T.x;
            q.push(T);
        }
        if(n==1) {
            node T=q.top();
            sum=T.x;
        }
        printf("%lld
",sum);
    }
    return 0;
}
//最大的一块和次大的那一块之和加入下一次的 运算
//可用优先队列

题目地址:【POJ】[3253]Fence Repair

原文地址:https://www.cnblogs.com/BoilTask/p/12569644.html