/**
*@ author StormMaybin
*@ date 2016-09-27
*/
生命不息,奋斗不止!
题目分析
Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 41139 Accepted: 13409
Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input
3
8
5
8
Sample Output
34
Hint
He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
Source
经典贪心问题,每次合并两个最小的,然后将它们的和放入数组中,继续合并两个最小的,依次求解
演示代码
package com.stormma.poj;
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
public class Main3253
{
/**
* @param args
*/
private Scanner scan = null;
public Main3253 ()
{
scan = new Scanner(new BufferedInputStream(System.in));
while (scan.hasNext())
{
int n = scan.nextInt();
int [] md = new int [n];
for (int i = 0; i < n; i++)
md[i] = scan.nextInt();
long ans = 0;
while (n > 1)
{
int min1 = 0;
int min2 = 1;
if (md[min1] > md[min2])
swap(md, min1, min2);
for (int i = 2; i < n; i++)
{
if (md[i] < md[min1])
{
min2 = min1;
min1 = i;
}
else if (md[i] < md[min2])
min2 = i;
}
int t = md[min1] + md[min2];
ans += t;
if (min1 == n-1)
swap(md, min1, min2);
md[min1] = t;
md[min2] = md[n-1];
n--;
}
System.out.println(ans);
}
}
public void swap (int [] md, int x, int y)
{
int temp = md[x];
md[x] = md[y];
md[y] = temp;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Main3253();
}
}

博客介绍了POJ3253问题,即农民约翰需要修理围栏,购买了一块长木板,需要锯成N段,每段长度不同。农民唐提供锯木服务,按长度收费。博客讨论了如何通过贪心策略找到锯木的最低成本,并给出了样例输入输出及解题思路。

1万+

被折叠的 条评论
为什么被折叠?



