Advertisement
Guest User

ACP - Assoc of Computing

a guest
Oct 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AssociationForComputing
  4. {
  5.     public static void main (String[] args)
  6.     {
  7.         AssociationForComputing sorter = new AssociationForComputing();
  8.         Scanner keyin = new Scanner(System.in);
  9.  
  10.         int N = keyin.nextInt();
  11.         int p = keyin.nextInt();
  12.         int mins[] = new int[N];
  13.         int pt = 0;
  14.         int currSum = 0;
  15.         int result[] = new int[N];
  16.         int k = 0;
  17.  
  18.         for (int i = 0; i < N; i++)
  19.         {
  20.             mins[i] = keyin.nextInt();
  21.         }
  22.  
  23.         if (mins[p] < 300)
  24.         {
  25.             result[0] = mins[p];
  26.             mins[p] = -1;
  27.             sorter.sort(mins, 0, N-1);
  28.    
  29.             currSum += result[0];
  30.             k = 1;
  31.             while(currSum < 300)
  32.             {
  33.                 result[k] = mins[k];
  34.                 currSum += mins[k];
  35.                 k++;
  36.             }
  37.    
  38.             for (int i = 0; i < k; i++)
  39.             {
  40.                 for (int j = 0; j < i + 1; j++)
  41.                 {
  42.                     pt += result[j];
  43.                 }
  44.             }            
  45.         }
  46.  
  47.         System.out.println(String.format("%d %d", k, pt));
  48.     }
  49.  
  50.     void merge(int arr[], int l, int m, int r)
  51.     {
  52.         // Find sizes of two subarrays to be merged
  53.         int n1 = m - l + 1;
  54.         int n2 = r - m;
  55.  
  56.         /* Create temp arrays */
  57.         int L[] = new int [n1];
  58.         int R[] = new int [n2];
  59.  
  60.         /*Copy data to temp arrays*/
  61.         for (int i=0; i<n1; ++i)
  62.             L[i] = arr[l + i];
  63.         for (int j=0; j<n2; ++j)
  64.             R[j] = arr[m + 1+ j];
  65.  
  66.  
  67.         /* Merge the temp arrays */
  68.  
  69.         // Initial indexes of first and second subarrays
  70.         int i = 0, j = 0;
  71.  
  72.         // Initial index of merged subarry array
  73.         int k = l;
  74.         while (i < n1 && j < n2)
  75.         {
  76.             if (L[i] <= R[j])
  77.             {
  78.                 arr[k] = L[i];
  79.                 i++;
  80.             }
  81.             else
  82.             {
  83.                 arr[k] = R[j];
  84.                 j++;
  85.             }
  86.             k++;
  87.         }
  88.  
  89.         /* Copy remaining elements of L[] if any */
  90.         while (i < n1)
  91.         {
  92.             arr[k] = L[i];
  93.             i++;
  94.             k++;
  95.         }
  96.  
  97.         /* Copy remaining elements of R[] if any */
  98.         while (j < n2)
  99.         {
  100.             arr[k] = R[j];
  101.             j++;
  102.             k++;
  103.         }
  104.     }
  105.  
  106.     // Main function that sorts arr[l..r] using
  107.     // merge()
  108.     void sort(int arr[], int l, int r)
  109.     {
  110.         if (l < r)
  111.         {
  112.             // Find the middle point
  113.             int m = (l+r)/2;
  114.  
  115.             // Sort first and second halves
  116.             sort(arr, l, m);
  117.             sort(arr , m+1, r);
  118.  
  119.             // Merge the sorted halves
  120.             merge(arr, l, m, r);
  121.        
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement