【POJ】[3253]Fence Repair

文章字数:226

问题描述

问题分析

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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\n",sum);
	}
	return 0;
}
//最大的一块和次大的那一块之和加入下一次的 运算
//可用优先队列

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

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...