knyazer

Untitled

Apr 15th, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner in = new Scanner(System.in);
  9.  
  10.         int n = in.nextInt();
  11.         int m = in.nextInt();
  12.  
  13.         int[][] arr = new int[n][m];
  14.         for (int i = 0; i < n; i++) {
  15.             for (int j = 0; j < m; j++) {
  16.                 arr[i][j] = in.nextInt();
  17.             }
  18.         }
  19.  
  20.  
  21.         ArrayList minsX;
  22.         ArrayList minsY;
  23.  
  24.         ArrayList gMinsX = new ArrayList();
  25.         ArrayList gMinsY = new ArrayList();
  26.         for (int j = 0; j < n; j++) {
  27.             minsX = new ArrayList();
  28.             minsY = new ArrayList();
  29.             int min = 1000000;
  30.             for (int i = 0; i < m; i++) {
  31.                 if (arr[j][i] == min) {
  32.                     minsX.add(j);
  33.                     minsY.add(i);
  34.                 }
  35.                 if (arr[j][i] < min) {
  36.                     min = arr[j][i];
  37.  
  38.                     minsX = new ArrayList();
  39.                     minsY = new ArrayList();
  40.                     minsX.add(j);
  41.                     minsY.add(i);
  42.                 }
  43.             }
  44.  
  45.             gMinsX.addAll(minsX);
  46.             gMinsY.addAll(minsY);
  47.         }
  48.  
  49.         ArrayList maxsX;
  50.         ArrayList maxsY;
  51.  
  52.         ArrayList gMaxsX = new ArrayList();
  53.         ArrayList gMaxsY = new ArrayList();
  54.         for (int i = 0; i < m; i++) {
  55.             maxsX = new ArrayList();
  56.             maxsY = new ArrayList();
  57.             int max = -1;
  58.             for (int j = 0; j < n; j++) {
  59.                 if (arr[j][i] == max) {
  60.                     maxsX.add(j);
  61.                     maxsY.add(i);
  62.                 }
  63.                 if (arr[j][i] > max) {
  64.                     max = arr[j][i];
  65.  
  66.                     maxsX = new ArrayList();
  67.                     maxsY = new ArrayList();
  68.                     maxsX.add(j);
  69.                     maxsY.add(i);
  70.                 }
  71.             }
  72.  
  73.             gMaxsX.addAll(maxsX);
  74.             gMaxsY.addAll(maxsY);
  75.         }
  76.  
  77.         int counter = 0;
  78.         for (int i = 0, li = gMinsX.size(); i < li; i++) {
  79.             for (int j = 0, lj = gMaxsX.size(); j < lj; j++) {
  80.                 if (gMinsX.get(i) == gMaxsX.get(j) &&
  81.                         gMinsY.get(i) == gMaxsY.get(j)) counter += 1;
  82.             }
  83.         }
  84.  
  85.         System.out.println(counter);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment