poj 3666 Making the Grade
来源:互联网 发布:博客打水软件 编辑:程序博客网 时间:2024/05/19 18:15
DP 有点难想,第一个要想到的是最终的结果必然是给定数字的一个子集
然后转化为求原数组的一个子集。由于原数组无序,没法求。所以排序后做DP
dp[i][j]是前i个数由排序后的数组中的前j个(不一定j个都用,但是第j个最大了) 组成时的最小值
dp[i][j] = min( min(dp[i-1][0]...dp[i-1][j]) + abs(arr2[j] - arr[i]); 可转化成一维数组
被屠。。。日
import java.io.*;import java.util.*;class problem2{ int n; int arr[]; int arr2[]; int dp[]; void solver() throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(reader.readLine()); n = Integer.valueOf(st.nextToken()); arr = new int[n]; arr2 = new int[n]; dp = new int[n]; for(int i=0;i<n;i++){ st = new StringTokenizer(reader.readLine()); arr[i] = Integer.valueOf(st.nextToken()); arr2[i] = arr[i]; } Arrays.sort(arr2); for(int i=0;i<n;i++){ dp[i] = Math.abs(arr2[i]-arr[0]); } int min = Integer.MAX_VALUE; for(int i=1;i<n;i++){ int tmp_min = Integer.MAX_VALUE; for(int j=0;j<n;j++){ tmp_min = Math.min(tmp_min, dp[j]); dp[j] = tmp_min + Math.abs(arr2[j]-arr[i]); } } for(int i=0;i<n;i++){ if(dp[i]<min) min = dp[i]; } System.out.println(min); }} public class tour { public static void main (String [] args) throws Exception { problem2 p = new problem2(); p.solver(); } }
0 0
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- poj 3666--Making the Grade
- POJ 3666 Making the Grade
- poj 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ-3666 Making the Grade
- POJ-3666 Making the Grade
- poj 3666 Making the Grade
- POJ 3666 Making the Grade
- POJ 3666 Making the Grade [DP]
- POJ 3666 Making the Grade(左偏树)
- POJ 3666 Making the Grade (dp)
- poj 3666 Making the Grade (动态规划)
- 【POJ 3666】Making the Grade(简单DP)
- 用scanf判断是不是行末尾
- 5月2日至7月初培训学习期间计划书
- Git工作结构
- VideoView的使用
- 转【玩转cocos2d-x之二十五】数据结构CCArray
- poj 3666 Making the Grade
- 调用DisplayMetrics类获取手机分辨率
- 看源代码那些事
- 简单html音乐播放器
- Android-Volley
- View的setWillNotDraw()
- 我自己研究开源项目源代码的两个重要习惯
- 湘潭大学oj1216
- 毕业设计-基于深度神经网络的语音关键词检出系统-使用python脚本作词频统计-TIMIT