Formatted question description: https://leetcode.ca/all/2102.html

# 2102. Sequentially Ordinal Rank Tracker (Hard)

A scenic location is represented by its name and attractiveness score, where name is a unique string among all locations and score is an integer. Locations can be ranked from the best to the worst. The higher the score, the better the location. If the scores of two locations are equal, then the location with the lexicographically smaller name is better.

You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports:

• Adding scenic locations, one at a time.
• Querying the ith best location of all locations already added, where i is the number of times the system has been queried (including the current query).
• For example, when the system is queried for the 4th time, it returns the 4th best location of all locations already added.

Note that the test data are generated so that at any time, the number of queries does not exceed the number of locations added to the system.

Implement the SORTracker class:

• SORTracker() Initializes the tracker system.
• void add(string name, int score) Adds a scenic location with name and score to the system.
• string get() Queries and returns the ith best location, where i is the number of times this method has been invoked (including this invocation).

Example 1:

Input
[[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]
Output

Explanation
SORTracker tracker = new SORTracker(); // Initialize the tracker system.
tracker.add("branford", 3); // Add location with name="branford" and score=3 to the system.
tracker.get();              // The sorted locations, from best to worst, are: branford, bradford.
// Note that branford precedes bradford due to its higher score (3 > 2).
// This is the 1st time get() is called, so return the best location: "branford".
tracker.add("alps", 2);     // Add location with name="alps" and score=2 to the system.
tracker.get();              // Sorted locations: branford, alps, bradford.
// Note that alps precedes bradford even though they have the same score (2).
// This is because "alps" is lexicographically smaller than "bradford".
// Return the 2nd best location "alps", as it is the 2nd time get() is called.
tracker.add("orland", 2);   // Add location with name="orland" and score=2 to the system.
tracker.get();              // Sorted locations: branford, alps, bradford, orland.
// Return "bradford", as it is the 3rd time get() is called.
tracker.add("orlando", 3);  // Add location with name="orlando" and score=3 to the system.
tracker.get();              // Sorted locations: branford, orlando, alps, bradford, orland.
tracker.add("alpine", 2);   // Add location with name="alpine" and score=2 to the system.
tracker.get();              // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
tracker.get();              // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "orland".


Constraints:

• name consists of lowercase English letters, and is unique among all locations.
• 1 <= name.length <= 10
• 1 <= score <= 105
• At any time, the number of calls to get does not exceed the number of calls to add.
• At most 4 * 104 calls in total will be made to add and get.

Similar Questions:

## Solution 1. Set + Iterator

If we create custom object for the location, we need to add quite a few lines of code including the struct definition and the custom comparator. Instead, we can simply use pair<int, string> and use its builtin sorting.

Since score should be sorted in descending order, we negate all the scores before adding them into the set.

For set’s iterator, even though we can’t do it = it + 1, we can do ++it.

// OJ: https://leetcode.com/problems/sequentially-ordinal-rank-tracker/
// Time:
//      SORTracker: O(1)
//      get: O(1)
// Space: O(N)
class SORTracker {
set<pair<int, string>> s;
set<pair<int, string>>::iterator it;
public:
SORTracker() {
s.emplace(0, ""); // sentinel
it = s.begin();
}
void add(string name, int score) {
pair<int, string> p = {-score, name};
s.insert(p);
if (p < *it) --it;
}
string get() {
return it++->second;
}
};


Or without sentinel

// OJ: https://leetcode.com/problems/sequentially-ordinal-rank-tracker/
// Time:
//      SORTracker: O(1)
//      get: O(1)
// Space: O(N)
class SORTracker {
set<pair<int, string>> s;
set<pair<int, string>>::iterator it = s.end();
public:
SORTracker() {}
void add(string name, int score) {
pair<int, string> p = {-score, name};
s.insert(p);
if (it == s.end() || p < *it) --it;
}
string get() {
return it++->second;
}
};