Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴Java代码。 O(n*log n) ,带注释

Posted by haqishen at 2015-01-05 18:43:59 on Problem 3253 and last updated at 2015-01-05 18:45:02
import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

//              创建优先队列,不会用的自行看手册
		PriorityQueue<Long> pq = new PriorityQueue<Long>();

//              输入数据
		int N = s.nextInt();
		for(int i = 0; i < N; i++)
			pq.add(s.nextLong());
		
//              首先把最小的两段木头拼在一起后算在cost里,这就是最后一刀的最优解。
//              然后把拼接后的木段放入优先队列中,重复第一步
//              直到优先队列中只剩下最后一个数字。
		long cost = 0;
		while(N > 1){
			long c = pq.poll() + pq.poll();
			cost += c;
			pq.add(c);
			N--;
		}
		System.out.println(cost);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator