コード例 #1
0
  public void testSubMultisetSize() {
    TreeMultiset<String> ms = TreeMultiset.create();
    ms.add("a", Integer.MAX_VALUE);
    ms.add("b", Integer.MAX_VALUE);
    ms.add("c", 3);

    assertEquals(Integer.MAX_VALUE, ms.count("a"));
    assertEquals(Integer.MAX_VALUE, ms.count("b"));
    assertEquals(3, ms.count("c"));

    assertEquals(Integer.MAX_VALUE, ms.headMultiset("c", CLOSED).size());
    assertEquals(Integer.MAX_VALUE, ms.headMultiset("b", CLOSED).size());
    assertEquals(Integer.MAX_VALUE, ms.headMultiset("a", CLOSED).size());

    assertEquals(3, ms.tailMultiset("c", CLOSED).size());
    assertEquals(Integer.MAX_VALUE, ms.tailMultiset("b", CLOSED).size());
    assertEquals(Integer.MAX_VALUE, ms.tailMultiset("a", CLOSED).size());
  }
コード例 #2
0
ファイル: Main.java プロジェクト: eric7237cire/CodeJam
  public String handleCase2(InputData in) {

    /**
     * The idea is we put the levels in 2 sets.
     *
     * <p>One is ordered by least set 2 star prereq first ; breaking ties by higher 1 star.
     *
     * <p>TODO why 1 star sort?
     */
    Ordering<Level> set2Ordering =
        Ordering.from(
            new Comparator<Level>() {

              @Override
              public int compare(Level o1, Level o2) {
                return ComparisonChain.start()
                    .compare(o1.twoStarPrereq, o2.twoStarPrereq)
                    .compare(o2.oneStarPrereq, o1.oneStarPrereq)
                    .result();
              }
            });

    TreeMultiset<Level> set2star = TreeMultiset.create(set2Ordering);

    /**
     * If we need to play a 1 star, play the one with the highest 2 star req. This is because we may
     * be able to play a lower 2 star req in 1 round later on.
     */
    TreeMultiset<Level> set1star =
        TreeMultiset.create(
            new Comparator<Level>() {

              @Override
              public int compare(Level o1, Level o2) {
                return ComparisonChain.start()
                    .compare(o1.oneStarPrereq, o2.oneStarPrereq)
                    .compare(o2.twoStarPrereq, o1.twoStarPrereq)
                    .result();
              }
            });

    set1star.addAll(in.levels);
    set2star.addAll(in.levels);

    int score = 0;
    int count = 0;

    while (!set2star.isEmpty()) {

      Level nextSet2 = set2star.firstEntry().getElement();

      if (nextSet2.twoStarPrereq <= score) {
        ++count;

        if (set1star.contains(nextSet2)) {
          score++;
          set1star.remove(nextSet2);
        }

        set2star.remove(nextSet2);
        score++;

        continue;
      }

      List<Level> set1Choices =
          new ArrayList<>(set1star.headMultiset(new Level(score, 0), BoundType.CLOSED));

      Collections.sort(set1Choices, set2Ordering);

      if (nextSet2.twoStarPrereq > score) {

        // Need to take from set1 with the highest possible round 2 score
        if (set1Choices.isEmpty()) {
          return String.format("Case #%d: Too Bad", in.testCase);
        }

        ++count;
        score += 1;

        Level round1 = set1Choices.remove(set1Choices.size() - 1);
        set1star.remove(round1);
        continue;
      }
    }

    // in.levelMin.subList()

    return String.format("Case #%d: %s", in.testCase, count);
  }