Account
Draft saved on this device
▤ DescriptionOriginal CppValley problem
Design PatternsHard

Implement a Strategy-Based Order Router

Switch between routing policies without changing the order-processing interface.

A router processes orders using one of two policies, LOWEST_FEE or FASTEST. Each venue has a unique ID, an integer fee, and an integer latency. POLICY name changes the active policy. ROUTE selects the venue with minimum fee for LOWEST_FEE or minimum latency for FASTEST; ties go to the smaller venue ID. Print the selected venue ID for each ROUTE. The policy is initially LOWEST_FEE. Design your implementation so policies can be changed independently of the routing interface.

Input format

First line: V Q. Next V lines: id fee latency. Next Q lines: POLICY LOWEST_FEE, POLICY FASTEST, or ROUTE.

Output format

For each ROUTE print the selected venue ID.

Examples

Example 1
Input
3 4
10 4 9
20 3 8
30 3 5
ROUTE
POLICY FASTEST
ROUTE
ROUTE
Expected output
20
30
30

Fee tie goes to venue 20. Lowest latency is venue 30.

Constraints

  • 1 ≤ V ≤ 100000
  • 1 ≤ Q ≤ 100000
  • 1 ≤ id ≤ 10^9; IDs are unique
  • 0 ≤ fee, latency ≤ 10^9

Related concepts

StrategyRoutingTrading Systems

This early-access exercise uses standard input/output. More advanced concurrency challenges will use specialized class interfaces, instrumentation, and stress tests once the judging infrastructure is ready.

⌘ CodeC++17
GNU C++17 · main.cpp

Loading Monaco C++ editor…

Checking judge...

Checking whether the external C++ judge is available.

⌁ Test cases & resultsPublic samples: 1
Input
3 4
10 4 9
20 3 8
30 3 5
ROUTE
POLICY FASTEST
ROUTE
ROUTE
Expected output
20
30
30
Execution requires a secure external judge.