thelili018's blog

By thelili018, history, 8 years ago, In English

import java.util.Scanner;

public class Taxi2 {

public static void main (String[] args) {

    int n, x, count;
    int[] a;

    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    a = new int[4];

    for(int i = 0; i < n; i++) {
       x = sc.nextInt();
       switch(x) {
       case 1: a[0]++; break;
       case 2: a[1]++; break;
       case 3: a[2]++; break;
       case 4: a[3]++; break;
       }
    }

    count = a[3];

    if(a[2] != 0 && a[0] != 0) {
       x = (a[2] >= a[0]) ? a[0] : a[2];
       for(int i = 0; i < x; i++) {
         a[2]--;
         a[0]--;
         count++;
       }
    }

    count += a[2];
    count += a[1] / 2;
    a[1] = a[1] % 2;

    if(a[1] != 0 && a[0] >= 2) {
       a[1]--;
       a[0] = a[0] - 2;
       count++;
    }
    else
       count += a[1];

    count += a[0] / 4;
    if(a[0] % 4 != 0)
       count++;

    System.out.println(count);
}

}

  • Vote: I like it
  • -6
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
Rev. 3   Vote: I like it +8 Vote: I do not like it

deleted

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Is that why it fails on test 5? It's a time-complexity issue?

    • »
      »
      »
      8 years ago, # ^ |
      Rev. 2   Vote: I like it +5 Vote: I do not like it

      For test 5 answer is 1 but your code is giving output as 2.

      Test case 5 is

      2

      2 1

      You can put both groups in one taxi so answer is one.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You cannot solve it in O(1) just because you have to input N numbers...