programming challenge – C++, sort integers using knowledge of entire vector

I am solving the “Sort” problem on Kattis.

Mirko is a great code breaker. He knows any cipher in the world can be broken by frequency analysis. He has completely the wrong idea what frequency analysis is, however.
He intercepted an enemy message. The message consists of N
numbers, smaller than or equal to C.
Mirko belives freqency analysis consists of sorting this sequence so that more frequent numbers appear before less frequent ones.
Formally, the sequence must be sorted so that given any two numbers X
and Y, X appears before Y if the number of times X appears in the original sequence is larger than the number of time Y does. If the number of appearances is equal, the number whose value appears sooner in the input should appear sooner in the sorted sequence.
Help Mirko by creating a “frequency sorter”.
First line of input contains two integers, N (1≤N≤1000), the length of the message, and C (1≤C≤1000000000), the number from the task description above.
The next line contains N positive integers smaller than or equal to C, the message itself.

Basically, the problem is as follows. Let xs be a nonempty vector of positive integers. There are only few integers in this vector, but they have a big range. (The maximum value c is given in the problem, but my code does not use the information.) Sort the integers according to the following criteria.

  1. For any two elements x and y of xs, if x occurs more often than y, then x appears first; if y appears more often, y appears first.
  2. If x and y appear equally often, then x occurs first if the very first occurrence of x is earlier than that of y.

I use a comparison sort (provided by the C++ runtime) with a smart comparator. This comparator knows the frequency and the index of the first appearance of every element. This information is not inherent to the integers. Rather, it depends entirely on their location within the vector. This contextual information is generated when a comparator is created for a given vector. Upon application on elements x and y, it returns true if x must appear before y.

I have used custom comparators before, but never have I used anything that contains state. In the disassembly with -Os I see many copy and move constructors called under sort(vector<unsigned> &). The code passes all tests, and it’s not slow.

But I wonder why the disassembly reveals so many copy and move calls, and whether this pattern of using heavy comparators is discouraged in C++. If this looks like a known pattern, I want to know its name. I appreciate general comments and insights.

#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>

typedef std::vector<unsigned> vector;

/// Comparison based on knowledge of the entire vector
struct compare {
    std::multiset<unsigned> bag;
    std::map<unsigned, size_t> indices;

    /// Extract frequency and initial index of every element.
    explicit compare(vector const &xs) {
        for (size_t i = 0u; i < xs.size(); ++i) {
            unsigned const x = xs(i);
            if (!indices.count(x)) {
                indices(x) = i;

    /// True if `x` must go before `y`.
    ((nodiscard)) bool operator()(unsigned x, unsigned y) const {
        return bag.count(x) > bag.count(y)
               || (bag.count(x) == bag.count(y) && <;

static void sort(vector &v) {
    compare c(v);
    std::sort(v.begin(), v.end(), c);

int main() {
    vector v;
        // Get `n` unsigned integers from console.
        // Unused: `c` (upper bound for integers)
        unsigned n, c;
        std::cin >> n >> c;
        while (n--) {
            unsigned x;
            std::cin >> x;
    // Sort according to the problem description
    // Print all
    for (unsigned const x : v) {
        std::cout << x << ' ';
    return 0;

Knowledge Base | Web Hosting Talk

Knowledge Base | Web Hosting Talk

var sidebar_align = ‘right’;
var content_container_margin = parseInt(‘350px’);
var sidebar_width = parseInt(‘330px’);

  1. Question Knowledge Base

    What’s the best knowledge base script these days?

Similar Threads

  1. Replies: 1

    Last Post: 01-31-2002, 12:10 PM

  2. Replies: 2

    Last Post: 12-05-2001, 01:03 AM

  3. Replies: 4

    Last Post: 07-10-2001, 11:25 PM

  4. Replies: 0

    Last Post: 07-10-2001, 10:28 PM

  5. Replies: 4

    Last Post: 02-02-2001, 06:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

multi factor – Does mobile biometric authentication count as Knowledge, Possession, or Inherence authentication

Apple claims in this year’s WWDC that Face ID and Touch ID count for both Possession and Inherence identity factors, because they are using Biometrics (Inherence) to access the secure element on your phone (Possession) to retrieve a unique key. See here:

I think both claims are a stretch. For Inherence, yes, you have proved to iOS that the person who set up Face ID is again using the phone, and therefore given access to the secure key. So iOS can claim Inherence. But your app has no proof that the human possessing the phone is actually your user. Hence my app considers mobile local authentication merely a convenient Knowledge factor–a shortcut for your username and password that resolves common credential problems like human forgetfulness.

As for Possession, again, I think the claim is a stretch unless before writing the unique key to the phone’s secure element you somehow prove that the possessor of the phone is your actual intended user. I suppose if you enable Face ID login immediately after account creation you can have this proof–the brand-new user gets to declare this is their phone like they get to choose their username and password. But on any login beyond the first you would have to acquire proof of Possession using an existing factor before you could grant a new Possession factor. Else a fraudster who steals credentials can claim their phone is a Possession factor by enabling Face ID; a situation made extra problematic by Apple’s claim that Face ID also counts as Inherence!

Am I wrong in this assessment? Which of Knowledge, Possession, and Inherence should an app developer grant mobile local biometric authentication?

Required knowledge for web hosting

Registration at Web Hosting Talk is completely free and takes only a few seconds. By registering you’ll gain:

– Full Posting Privileges.

– Access to Private Messaging.

– Optional Email Notification.

– Ability to Fully Participate.

– And Much More.

Register Now, or check out the Site Tour and find out everything Web Hosting Talk has to offer.

Earn $ 500 + per day using your knowledge.

Hello dear friend. Like me, you have been deceived on the Internet. I know this because I myself was deceived. But of all the deceptions, I selected only truthful training. I went through them all. I can recommend any of these courses with confidence. Choose one of them on my twitter page and I guarantee you a very rich future! Join now, do not be lazy, study and you will not regret it!


privacy – What consensus algorithm is MimbleWimble/Grin using? Does it use Zero Knowledge Proofs as well?

A Mimblewimble blockchain relies on two complementary aspects to provide security: Pedersen Commitments and range proofs (in the form of Bulletproof range proofs). Bulletproofs do not require a trusted setup. They rely only on the discrete logarithm assumption, and are made non-interactive using the Fiat-Shamir heuristic.

Pedersen Commitments provide perfectly hiding and computationally binding commitments. Since Mimblewimble commitments are totally confidential and ownership cannot be proved, anyone can try to spend or mess with unspent coins embedded in those commitments. Fortunately, any new UTXO requires a range proof, and this is impossible to create if the input commitment cannot be opened.

A Mimblewimble blockchain grows with the size of the UTXO set. Using Bulletproofs, it would only grow with the number of transactions that have unspent outputs, which is much smaller than the size of the UTXO set.

CoinJoin is a technique to aggregate multiple payments from multiple senders into one unified transaction. Dash deployed an improved version of CoinJoin earlier. Mimblewimble can do CoinJoin non interactively and verifiably in public. Hence Mimblewimble can be viewed as a privacy preserving cryptocurrency approach using Non Interactive CoinJoin technique.

Self Improvement – Online course for backend engineering with Java / Kotlin knowledge

I'm not sure if this question belongs to this community, but I couldn't find a better one.

I have been developing Android apps professionally with Java / Kotlin for several years and now I want to start learning the modern backend development on JVM.

I asked around and heard good opinions about the Udacity Java Backend Developer course:–nd035. Unfortunately it is being reconstructed and I cannot start it.

Can you recommend another similar online course?

dnd 3.5e – Is there a way to add both knowledge (religion) and knowledge (the levels) to the magic thief skill list?

… except to take Skill Knowledge twice? (or Skill Knowledge + Apprentice)

I want this character to enter level 6 in Ur Priest, so he needs maximum ranks in knowledge (religion) and 5 in knowledge (levels), none of which are class skills for magic thief. And both achievements go into qualification. (Yes, I know that the Otyugh Hole Iron Will or Mistake could bring bonus benefits, but these are inelegant.)

As a human being, the character would have a free feat to add the two as class skills. Is there an achievement that could do that?

Or is there a race where you have class skills by default? Or for a race that receives one of them as a class skill and one of the services that I need as a bonus service?

DreamProxies - Cheapest USA Elite Private Proxies 100 Private Proxies 200 Private Proxies 400 Private Proxies 1000 Private Proxies 2000 Private Proxies - Buy Cheap Private Proxies Buy 50 Private Proxies Buy 100 Private Proxies Buy 200 Private Proxies Buy 500 Private Proxies Buy 1000 Private Proxies Buy 2000 Private Proxies ProxiesLive New Proxy Lists Every Day Proxies123