Skip to main content
Code Review

Return to Revisions

6 of 6
added 248 characters in body
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
  1. Don't force a class where a function is perfectly fine.

  2. The namespace std is not designed for wholesale importation, see "Why is "using namespace std" considered bad practice? " for more detail.

  3. Identifiers starting with an underscore followed by an uppercase letter are reserved.

  4. Input is generally unreliable. But you don't test for error at all.

  5. Always recomputing the current result from first principles, instead of just updating it with the newest addition, is a waste of time.

  6. Flushing is expensive, so avoid std::endl and explicitly request it with std::flush where unavoidable.

  7. You should probably follow the requested output-format...

  8. You know that int in unsigned int is implicit?

  9. In C++ and C99+, return 0; is implicit for main().

  10. I wonder why you sometimes surround binary operators with space, and sometimes don't.

  11. Having over-long lines is very cumbersome to read. Admittedly, you need not make a hard cut at 79 nowadays.

  12. You should double-check whether duplicate inputs can occurr (there are none in the example), and if so whether they should cause anything but repeated output.
    I assumed duplicated possible, and they only cause output.

  • Duplicates have an effect would destroy ranges instead of adding them, thus needing an ordered std::set.
  • Duplicates not possible would mean the std::unordered_set can be dispensed with.
#include <cstdlib>
#include <iostream>
#include <unordered_set>
int main() {
 unsigned n, m, x;
 std::cin >> n >> m;
 if (!std::cin || !n) {
 std::cerr << "Bad Input!\n";
 return EXIT_FAILURE;
 }
 std::unordered_set<unsigned> nums;
 auto r = 0ULL;
 while (--m) {
 std::cin >> x;
 if (!std::cin || x < 1 || x > n) {
 std::cerr << "Bad Input!\n";
 return EXIT_FAILURE;
 }
 auto [iter, created] = nums.insert(x);
 if (created)
 r += 2 * x;
 std::cout << r << '\n';
 }
}
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
default

AltStyle によって変換されたページ (->オリジナル) /