题目大意,有1<=N<=1000座山,每座山高0<=H<=100之间,求将所有山削低或者填高,使它们的高度差在小于等于17范围内,求达到此目标的最小成本。
注意:成本是按照单位平方的。
注意数据范围,可以遍历,从0开始到100,最多可以有100-17个相差17的单位区间遍历1000座山,也就是说将1000座山都比较一下做到在每个相差17的单位区间内的最小成本就行。
/* ID: wanglan1 LANG: JAVA TASK: skidesign */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class skidesign { public static void main(String args[]) throws IOException { BufferedReader f = new BufferedReader(new FileReader("skidesign.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("skidesign.out"))); int len = Integer.valueOf(f.readLine()); int[] mm = new int[len]; int max = 100, min = 1; for (int i = 0; i < len; i++) { mm[i] = Integer.valueOf(f.readLine()); if (max < mm[i]) max = mm[i]; if (min > mm[i]) min = mm[i]; } f.close(); int minn = 1000000, cost; for (int i = min; i <= max - 17; i++) { cost = 0; for (int k = 0; k < len; k++) { int high = mm[k]; if (high < i) cost += (i - high) * (i - high); else if (high > i + 17) cost += (high - i - 17) * (high - i - 17); } if (minn > cost) minn = cost; } out.println(minn); out.close(); } }