Mr_HO1A

Movie Sequence Question Mockvita

Jun 8th, 2019
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.08 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class MovieSequence{
  5.  
  6.      public static void main(String []args){
  7.         Scanner sc = new Scanner(System.in);
  8.         String m1 = sc.next();
  9.         String m2 = sc.next();
  10.         String m3 = sc.next();
  11.         String m4 = sc.next();
  12.         sc.nextLine();  //consume new-line
  13.         char g1 = sc.next().charAt(0);
  14.         char g2 = sc.next().charAt(0);
  15.         char g3 = sc.next().charAt(0);
  16.         char g4 = sc.next().charAt(0);
  17.         sc.nextLine();
  18.         int p1 = sc.nextInt();
  19.         int p2 = sc.nextInt();
  20.         int p3 = sc.nextInt();
  21.         int p4 = sc.nextInt();
  22.         sc.nextLine();
  23.         int n = sc.nextInt();
  24.         sc.nextLine();
  25.         int[] aud = new int[n];
  26.         for(int i = 0; i<n; i++){
  27.             aud[i]=sc.nextInt();
  28.         }
  29.        
  30.         /*
  31.          A' grade : Age Limit : 24-50 (24 and 50 included)
  32.         'B' grade : Age Limit : 15-25 (15 and 25 included)
  33.    
  34.         'C' grade : Age Limit : 3-18 (3 and 18 included)
  35.    
  36.         'D' grade : Age Limit : 45-70 (45 and 70 included)
  37.        */
  38.        
  39.         ArrayList<Integer> am1 = new ArrayList<Integer>(); //audience of movie 1
  40.         ArrayList<Integer> am2 = new ArrayList<Integer>();
  41.         ArrayList<Integer> am3 = new ArrayList<Integer>();
  42.         ArrayList<Integer> am4 = new ArrayList<Integer>();
  43.  
  44.         am1 = screenOutAudienceForMovie(g1,aud);
  45.         am2 = screenOutAudienceForMovie(g2,aud);
  46.         am3 = screenOutAudienceForMovie(g3,aud);
  47.         am4 = screenOutAudienceForMovie(g4,aud);
  48.        
  49.         /*
  50.          People between age 3-20 (both included) will have school in the morning, so will not be able to attend morning show.
  51.          People between age 21-40 (both included) will be at their jobs in the afternoon so will not be able to attend afternoon show.
  52.    
  53.          People between age 41-49 (both included) will not be able to watch movie in Evening show.
  54.    
  55.          People between age 50-70 (both included) will not be able to watch movie in Night show.
  56.         */
  57.        
  58.         ArrayList<Integer> pm1 = new ArrayList<Integer>(); //profit of movie 1, index 0 is morning, index 3 is night
  59.         ArrayList<Integer> pm2 = new ArrayList<Integer>();
  60.         ArrayList<Integer> pm3 = new ArrayList<Integer>();
  61.         ArrayList<Integer> pm4 = new ArrayList<Integer>();
  62.        
  63.         pm1 = calculateProfit(am1,p1);
  64.         pm2 = calculateProfit(am2,p2);
  65.         pm3 = calculateProfit(am3,p3);
  66.         pm4 = calculateProfit(am4,p4);
  67.        
  68.         ArrayList<String> generated = new ArrayList<String>();
  69.         //ArrayList<Integer> totalProfit = new ArrayList<String>();
  70.         int maxProfit = 0;
  71.         for(int i=0;i<4;i++){
  72.             for(int j=0; j<4; j++){
  73.                 if(j==i)
  74.                     continue;
  75.                 for(int k=0; k<4; k++){
  76.                     if(k==j||k==i)
  77.                         continue;
  78.                     for(int l=0; l<4; l++){
  79.                         if(l==k||l==j||l==i)
  80.                             continue;
  81.                         int tempTotalProfit = 0;
  82.                         tempTotalProfit = pm1.get(i)+pm2.get(j)+pm3.get(k)+pm4.get(l);
  83.                         if(tempTotalProfit>maxProfit){
  84.                             maxProfit=tempTotalProfit;
  85.                             generated.clear();
  86.                             //Generate the string of movies and store it
  87.                             String temp="";    
  88.                             int lookingFor = 0;
  89.                             while(lookingFor!=4){
  90.                                 if(i==lookingFor){
  91.                                     temp=temp+m1+" ";
  92.                                 }
  93.                                 else if(j==lookingFor){
  94.                                     temp=temp+m2+" ";
  95.                                 }
  96.                                 else if(k==lookingFor){
  97.                                     temp=temp+m3+" ";
  98.                                 }
  99.                                 else{
  100.                                     temp=temp+m4+" ";
  101.                                 }
  102.                                 lookingFor++;
  103.                             }
  104.                             generated.add(temp);
  105.                         }
  106.                         else if(tempTotalProfit==maxProfit){
  107.                             //Generate the string of movies and store it
  108.                             String temp="";    
  109.                             int lookingFor = 0;
  110.                             while(lookingFor!=4){
  111.                                 if(i==lookingFor){
  112.                                     temp=temp+m1+" ";
  113.                                 }
  114.                                 else if(j==lookingFor){
  115.                                     temp=temp+m2+" ";
  116.                                 }
  117.                                 else if(k==lookingFor){
  118.                                     temp=temp+m3+" ";
  119.                                 }
  120.                                 else{
  121.                                     temp=temp+m4+" ";
  122.                                 }
  123.                                 lookingFor++;
  124.                             }
  125.                             generated.add(temp);
  126.                         }
  127.                     }    
  128.                 }    
  129.             }
  130.            
  131.         }
  132.        
  133.         ArrayList<String> finalList = sortMyList(generated);
  134.         for(int i = 0; i<finalList.size(); i++){
  135.             System.out.println(finalList.get(i));
  136.         }
  137.        
  138.         System.out.println("Maximum Profit:"+maxProfit);
  139.        
  140.         sc.close();
  141.      }
  142.      
  143.      public static ArrayList<String> sortMyList(ArrayList<String> list){
  144.         final Pattern p = Pattern.compile("^\\d+");
  145.         ArrayList<String> result = new ArrayList<String>();
  146.         result = list;
  147.         String[] examples = {
  148.            "Movies1 Movies 2","Movies2 Movies 1"
  149.         };
  150.         Comparator<String> c = new Comparator<String>() {
  151.             @Override
  152.             public int compare(String object1, String object2) {
  153.                 Matcher m = p.matcher(object1);
  154.                 Integer number1 = null;
  155.                 if (!m.find()) {
  156.                     return object1.compareTo(object2);
  157.                 }
  158.                 else {
  159.                     Integer number2 = null;
  160.                     number1 = Integer.parseInt(m.group());
  161.                     m = p.matcher(object2);
  162.                     if (!m.find()) {
  163.                         return object1.compareTo(object2);
  164.                     }
  165.                     else {
  166.                         number2 = Integer.parseInt(m.group());
  167.                         int comparison = number1.compareTo(number2);
  168.                         if (comparison != 0) {
  169.                             return comparison;
  170.                         }
  171.                         else {
  172.                             return object1.compareTo(object2);
  173.                         }
  174.                     }
  175.                 }
  176.             }
  177.         };
  178.         Collections.sort(result, c);
  179.         return result;
  180.     }
  181.      
  182.      public static ArrayList<Integer> calculateProfit(ArrayList<Integer> am, int pm){
  183.          int len = am.size();
  184.          ArrayList<Integer> result = new ArrayList<Integer>();
  185.          ArrayList<Integer> morning = new ArrayList<Integer>();
  186.          ArrayList<Integer> afternoon = new ArrayList<Integer>();
  187.          ArrayList<Integer> eve = new ArrayList<Integer>();
  188.          ArrayList<Integer> night = new ArrayList<Integer>();
  189.          
  190.          for(int j=0; j<4; j++){
  191.              
  192.              switch(j){
  193.                 case 0:
  194.                     for(int i=0; i<len; i++){
  195.                         if(am.get(i)>20){
  196.                             morning.add(am.get(i));
  197.                         }
  198.                     }
  199.                     break;
  200.                 case 1:
  201.                     for(int i=0; i<len; i++){
  202.                         if((am.get(i)>2 && am.get(i)<21)||(am.get(i)>40)){
  203.                             afternoon.add(am.get(i));
  204.                         }
  205.                     }
  206.                     break;
  207.                 case 2:
  208.                     for(int i=0; i<len; i++){
  209.                         if((am.get(i)>2 && am.get(i)<41)||(am.get(i)>49)){
  210.                             eve.add(am.get(i));
  211.                         }
  212.                     }
  213.                     break;
  214.                 case 3:
  215.                     for(int i=0; i<len; i++){
  216.                         if(am.get(i)<50){
  217.                             night.add(am.get(i));
  218.                         }
  219.                     }
  220.                     break;
  221.              }
  222.          }
  223.          
  224.          result.add((morning.size())*pm);
  225.          result.add((afternoon.size())*pm);
  226.          result.add((eve.size())*pm);
  227.          result.add((night.size())*pm);
  228.          
  229.          return result;
  230.          
  231.      }
  232.      
  233.      public static ArrayList<Integer> screenOutAudienceForMovie(char grade, int[] aud){
  234.          int len = aud.length;
  235.          ArrayList<Integer> result = new ArrayList<Integer>();
  236.          switch(grade){
  237.             case 'A':
  238.                  for(int i = 0; i<len; i++){
  239.                      if(aud[i]>23 && aud[i]<51){
  240.                          result.add(aud[i]);
  241.                      }
  242.                  }
  243.                  break;
  244.             case 'B':
  245.                 for(int i = 0; i<len; i++){
  246.                      if(aud[i]>14 && aud[i]<26){
  247.                          result.add(aud[i]);
  248.                      }
  249.                  }
  250.                 break;
  251.             case 'C':
  252.                 for(int i = 0; i<len; i++){
  253.                      if(aud[i]>2 && aud[i]<19){
  254.                          result.add(aud[i]);
  255.                      }
  256.                  }
  257.                 break;
  258.             case 'D':
  259.                 for(int i = 0; i<len; i++){
  260.                      if(aud[i]>44 && aud[i]<71){
  261.                          result.add(aud[i]);
  262.                      }
  263.                  }
  264.                 break;
  265.          }
  266.          return result;
  267.      }
  268. }
Add Comment
Please, Sign In to add comment