Блог пользователя pribic

Автор pribic, история, 4 года назад, По-английски

Since we don't have multisets in Java, tried my luck to build one.

Reviews and feedbacks are welcome.

Basic idea is we keep another frequency map for each object. Whenever, we add an element, we update this frequency map as well. Whenever we remove, we check if that element now has frequency 0, if yes then only we remove it from set. Also we are using TreeSet so all the elements will be sorted and all TreeSet features are available out of the box.

I solved https://mirror.codeforces.com/contest/527/problem/C using this.

Submission — https://mirror.codeforces.com/contest/527/submission/116237858

static class MultiSet<T> extends TreeSet<T> {
    Map<T, Integer> frequency;

    public MultiSet() {
      this.frequency = new HashMap<>();
    }

    @Override
    public boolean add(T t) {
      frequency.put(t, frequency.getOrDefault(t, 0) + 1);
      return super.add(t);
    }


    @Override
    public boolean remove(Object o) {
      if (!frequency.containsKey(o))
        return false;
      frequency.put((T) o, frequency.getOrDefault(o, 1) - 1);
      if (frequency.get(o) == 0) {
        frequency.remove(o);
        super.remove(o);
      }
      return true;
    }

    @Override
    public String toString() {
      return new StringJoiner(", ", MultiSet.class.getSimpleName() + "[", "]")
        .add("frequency=" + frequency)
        .add(super.toString())
        .toString();
    }
  }

Полный текст и комментарии »

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

Автор pribic, история, 4 года назад, По-английски

I am getting wrong answer for https://mirror.codeforces.com/contest/1520/problem/F1. Can someone please look at my submission and help whats wrong here? I tried many samples but couldn't find the bug.

https://mirror.codeforces.com/contest/1520/submission/115514700

updated link with l = 1 — https://mirror.codeforces.com/contest/1520/submission/115621830

TIA

Полный текст и комментарии »

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

Автор pribic, история, 4 года назад, По-английски

I am trying to solve https://mirror.codeforces.com/contest/86/problem/D where I am using MO's traversal but I am keep getting TLE. Can someone please review my code and help me with slowness? I tried following things so far:

  1. Changed the way we read input. Reading entire line and converting them to int in memory
  2. Changed from Array to ArrayList since I heard Arrays.sort() has O(n^2) in some cases.

I am not sure if there is a flaw in logic or some library methods which is slowing down and giving TLE.

Submission: https://mirror.codeforces.com/contest/86/submission/109548850

Полный текст и комментарии »

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

Автор pribic, история, 4 года назад, По-английски

A lot of time we have seen problems where we need to output a YES or No. Usual way of doing this is flag ? "YES" : "NO" however many programming language supports directly printing boolean as True or False.

So is there any specific reason we are asked to print YES/NO instead TRUE/FALSE?

Here is a simple diff between both approach:

System.out.println(flag? "YES" : "NO" );

vs

System.out.println(flag);

Полный текст и комментарии »

  • Проголосовать: нравится
  • -25
  • Проголосовать: не нравится