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

Автор dev_il, история, 10 лет назад, По-английски

There is a lot of information about programming in C++ for competitions as well as almost all algorithm implementations are written in C++ language (e.g. e-maxx site, Topcoder tutorials etc.). So from there we can take some best practices of what to use or not to use when writing problem solutions.

For Java there is no such resources as I know. If not I would be really thankful to obtain links to them.

It's good to read codes in Java of target coders like Petr or Egor but it would be better to have it in one place (like e-maxx site).

Additionally I want to ask some questions interesting for me.

  1. How do you know that some problem need to be written in C++ instead of Java? For example 524F - And Yet Another Bracket Sequence that Petr wrote in C++ not Java. I don't see any specific data structure like std::bitset that Java don't have.

  2. Which tricks are not recommended to use in Java (like you need to pay attention to autoboxing, that may do your solution more slow unexpectedly) ?

  3. I like very much C++'s std::pair<T1, T2> and it seems that it's much more efficient that self-written Java analog (e.g. https://github.com/EgorKulikov/yaal/blob/master/lib/main/net/egork/collections/Pair.java). Is it true or not? I think so because of creation of Object for each element of Pair (that need to be done because generics in Java cannot be declared as Pair<int, int> but must be Pair<Integer, Integer>). So would be the implementation of Pair for each case int first, second, int first; long second; etc more efficient than such of Egor?

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

»
10 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For the 3rd: note that creation of Pairs themselfs is creation of objects too.

  • »
    »
    10 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Yes, I know.

    I would like to point to the difference: it's also object creation in C++, but in Java it needs 3 object to be created instead of 1 in C++.

    • »
      »
      »
      10 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      In c++ pair is transparent and it costs you the same as two ints inside (as long as you don't use dynamic allocation which you shouldn't. SO if you consider ints free there's no objects.

      In Java you'll pay for each pair created.

»
10 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

https://sites.google.com/site/indy256/ — a lot of algorithms in Java

»
10 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Petr's solution is the same as tourist & niyaz solution :) Look at #10384194 & #10401377

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I'm sorry I didn't catch your English but 1. Sid you mean that JAVA does not have BitSet? Because it does. I happened to solve a problem using BitSet in JAVA too: Problem Submission

  1. What's wrong with writing this small piece of code everytime, for the required type of Datatype? For example for 2 integers, is this really slow? I don't know so i'm asking really. Because i've used it plenty of times

[code] class pair { int first,second; pair(int first,int second) { this.first=first; this.second=second; } } [\code]

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Yes, you're right. Java has bitset, I didn't know that.

    What about Pair. I don't want to write class Pair every time even though it doesn't take too much time. It would be better to write one time Pair<S, T> imlpementation and use it every time because CHelper plug-in provide such functionality (to use pre-written code on contest without copy-paste).