【POJ】[3253]Fence Repair

Problem Description

Loading...

Problem Analysis

I think I’ve done a similar one before
But it seemed simpler than this

cpp
 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;
}
//最大的一块和次大的那一块之和加入下一次的 运算
//可用优先队列

Problem link: POJ 3253 Fence Repair

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...