标签:
1 import java.util.*; 2 public class Main { 3 public static void main(String aegs[]) { 4 Scanner sc = new Scanner(System.in); 5 int n, m; 6 int[] val = new int[1001]; 7 while (sc.hasNext()) { 8 n = sc.nextInt(); 9 m = sc.nextInt(); 10 for (int i = 0; i < n; ++i) { 11 val[i] = sc.nextInt(); 12 } 13 Map<String, Integer> mp = new <String, Integer>HashMap(); 14 String name; 15 for (int i = 0; i < m; ++i) { 16 name = sc.next(); 17 int num = mp.getOrDefault(name, 0) + 1; 18 mp.put(name, num); 19 } 20 Arrays.sort(val, 0, n); 21 List<Integer> list = new ArrayList<Integer>(mp.values()); 22 int size = list.size(); 23 Integer[] array = (Integer[])list.toArray(new Integer[size]); 24 Arrays.sort(array); 25 int max = 0, min = 0; 26 int i = 0; 27 for (Integer num : array) { 28 max += val[n - size + i] * num; 29 if (size - 1 - i >= 0) { 30 min += num * val[size - 1 - i]; 31 } 32 i++; 33 } 34 System.out.println(min + " " + max); 35 } 36 } 37 }
1 /** 2 * Created by Administrator on 2016/9/5. 3 */ 4 5 import java.util.Scanner; 6 7 public class Main{ 8 public static void main(String[] args){ 9 Scanner in =new Scanner(System.in); 10 while(in.hasNext()){ 11 int n=in.nextInt(); 12 int[] arr=new int[n]; 13 for(int i=0;i<n;i++){ 14 arr[i]=in.nextInt(); 15 } 16 System.out.println(getCount(arr)); 17 } 18 } 19 20 21 public static int getCount(int[] arr) { 22 if (arr == null || arr.length < 2) { 23 return 0; 24 } else { 25 int res = 0; 26 int[][] dp = getDP(arr); 27 for (int i = 0; i < dp.length; i++) { 28 for (int j = 0; j < dp.length; j++) { 29 res += dp[i][j]; 30 } 31 } 32 return res / 2; 33 } 34 } 35 36 public static int[][] getDP(int[] arr) { 37 int[][] dp = new int[arr.length][arr.length]; 38 dp[0][arr.length - 1] = 1; //头尾是相邻的,配对 39 dp[arr.length - 1][0] = 1; 40 for (int i = 0; i < dp.length; i++) { 41 for (int j = 0; j < dp.length; j++) { 42 if(dp[i][j] == 0){ 43 if(i == j){ 44 dp[i][j] = 0; //自己跟自己不配对 45 }else if (i == j - 1 || j == i - 1) { 46 dp[i][j] = 1; //相邻的配对 47 } else { 48 //如果dp[i][j-1]为1,则arr[j-1]满足arr[j - 1] <= Math.min(arr[i], arr[j])使arr[i]、arr[j]配对 49 if (j > 0 && dp[i][j - 1] == 1 50 && arr[j - 1] <= Math.min(arr[i], arr[j])) { 51 dp[i][j] = 1; 52 } 53 //如果dp[i-1][j]为1,则arr[i-1]满足arr[i - 1] <= Math.min(arr[i], arr[j])使arr[i]、arr[j]配对 54 if (i > 0 && dp[i - 1][j] == 1 55 && arr[i - 1] <= Math.min(arr[i], arr[j])) { 56 dp[i][j] = 1; 57 } 58 } 59 } 60 } 61 } 62 return dp; 63 } 64 }
标签:
原文地址:http://www.cnblogs.com/kimi9py/p/5845342.html