Guest User

sam

a guest
Oct 23rd, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. A sequence is given of positive integers N and one number K. Let all numbers of the sequence is highlighted a0 to an-1. First To define the sum of absolute differences as abs (a1-a0) + abs (a2-a1) + ... + abs (aN-1-aN-2). To choose exactly K consecutive numbers of the sequence, so that when you merge into one sequence, the sum of absolute differences is the maximum. Print this amount.
  2.  
  3. Input: The first line you are given two numbers N (1≤N≤100) and K (1≤K≤100, K≤N). In the second line you are given N positive integers, each number is less than 1,000.
  4.  
  5. Output: To print the required maximum sum of absolute differences.
  6.  
  7. Note: The numbers are taken in the order in which they are given in the first row. Do not change the order of the numbers in the new sequence.
  8.  
  9. Name of the class (Java): SumOfAbsoluteDifferences.
  10.  
  11. A partial solution: The task is considered solved if partly spent 5 test examples.
  12.  
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.InputStreamReader;
  16. import java.util.StringTokenizer;
  17.  
  18. public class SumOfAbsoluteDifferences {
  19.  
  20.         static int solve(int a[], int n, int k)
  21.         {
  22.                 int[][] mat = new int[a.length][k];
  23.                 int rez = 0;
  24.  
  25.                 for (int i = 1; i < n; i++)
  26.                 {
  27.                         for (int j = 1; j < k; j++)
  28.                         {
  29.                                 for (int k1 = 0; k1 < i; k1++)
  30.                                 {
  31.                                         int raz = Math.abs(a[i] - a[k1]);
  32.                                         if (mat[i][j] < mat[k1][j-1] + raz)
  33.                                                 mat[i][j] = mat[k1][j-1] + raz;
  34.                                         //System.out.printf("%d - %d = %d\n", a[i], a[k1], raz);
  35.                                 }
  36.                                 //System.out.println("--------------------------------");
  37.                                 if (mat[i][j] > rez) rez = mat[i][j];
  38.                         }
  39.                 }
  40.         /*
  41.                 for (int i = 0; i < n; i++)
  42.                 {
  43.                         for (int j = 0; j < k; j++)
  44.                                 System.out.print(mat[i][j] + " ");
  45.                         System.out.println("");
  46.                 }*/
  47.                 return rez;
  48.         }
  49.  
  50.         public static void main(String[] args) throws Exception {
  51.                 int i, j, k;
  52.  
  53.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  54.                 StringTokenizer st = new StringTokenizer(br.readLine());
  55.                 int N = Integer.parseInt(st.nextToken());
  56.                 int K = Integer.parseInt(st.nextToken());
  57.  
  58.                 int numbers[] = new int[N];
  59.  
  60.                 st = new StringTokenizer(br.readLine());
  61.                 for (i = 0; i < N; i++) {
  62.                         numbers[i] = Integer.parseInt(st.nextToken());
  63.                 }
  64.  
  65.                 int res = solve(numbers, N, K);
  66.                 System.out.println(res);
  67.  
  68.                 br.close();
  69.  
  70.         }
  71.  
  72. }
  73.  
  74.  
  75. Sample input
  76. 10 3
  77. 1 9 2 3 6 1 3 2 1 3
  78.  
  79. Sample output
  80. 16
Advertisement
Add Comment
Please, Sign In to add comment