公式

C - お得なショッピングモール巡り / Bargain Shopping Mall Tour 解説 by admin

Gemini 3.0 Flash

Overview

From \(N\) shopping malls, select at most \(K\) of them to maximize the value (net profit) obtained by subtracting “transportation and parking fees” from “the total discount gained from stores in the selected malls.”

Analysis

The key points for solving this problem can be summarized in the following three aspects:

  1. Which stores should you visit at each mall? Looking at the constraints, the discount \(P_{i,j}\) for each store is always at least \(1\). Therefore, once you decide to stop at a mall \(i\), shopping at all stores within that mall is the optimal strategy to maximize the profit from that mall. Thus, the profit (net profit) obtained from visiting mall \(i\) can be calculated as follows: $\(\text{(Net Profit)}_i = \left( \sum_{j=1}^{M_i} P_{i,j} \right) - C_i\)$

  2. Which malls should you visit? Only malls whose calculated “net profit” is positive (greater than \(0\)) are worth considering. Malls with negative net profit will decrease your total gain if visited, so they can be ignored.

  3. How to handle the constraint of at most \(K\) malls? You can visit at most \(K\) malls. Therefore, from the “positive net profits” calculated earlier, selecting up to \(K\) malls with the largest values and summing them gives the answer. If there are fewer than \(K\) malls with positive net profit, visiting all of them is optimal.

Algorithm

  1. For each mall \(i = 1, 2, \ldots, N\), perform the following:
    • Calculate the total discount of all stores in the mall: \(S_i = \sum P_{i,j}\).
    • Calculate the net profit: \(X_i = S_i - C_i\).
    • If \(X_i > 0\), add \(X_i\) to a list (or heap).
  2. Sort the stored list of net profits in descending order (largest first).
  3. Take at most \(K\) elements from the beginning of the list and output their sum.

Complexity

  • Time Complexity: \(O(\sum M_i + N \log K)\)
    • Reading all store discounts takes \(O(\sum M_i)\).
    • Extracting the top \(K\) elements from \(N\) elements takes \(O(N \log N)\) using sorting, or \(O(N \log K)\) using a heap.
  • Space Complexity: \(O(N)\)
    • Up to \(N\) elements of memory are used to store the net profit of each mall.

Implementation Tips

  • Large input data: Since \(N\) and \(\sum M_i\) can be as large as \(10^5\), using sys.stdin for fast input is recommended in Python.

  • Memory efficiency: The sample code uses generators (yield) and itertools.islice to process large amounts of data efficiently without converting everything into a list at once, keeping memory consumption low.

  • Retrieving the top \(K\) elements: Using heapq.nlargest(k, list) can sometimes retrieve the top \(K\) elements more efficiently than fully sorting the entire list.

    Source Code

import sys
import heapq
from itertools import islice

def solve():
    # Use a generator to fetch tokens from standard input one by one.
    # This approach is memory-efficient and differs from loading all data into a list.
    def token_stream():
        for line in sys.stdin:
            for word in line.split():
                yield int(word)

    tokens = token_stream()

    # Read the number of malls (N) and the maximum number of malls to visit (K)
    try:
        n = next(tokens)
        k = next(tokens)
    except StopIteration:
        return

    def get_mall_net_profits():
        # Iterate through the N malls provided in the input
        for _ in range(n):
            # Each mall starts with its cost (C_i) and number of shops (M_i)
            cost = next(tokens)
            m_i = next(tokens)
            
            # Sum up the benefits (P_i,j) of all shops in this mall.
            # islice(tokens, m_i) takes exactly the next m_i shop values from the generator.
            # Since shop benefits are at least 1, we always include all shops if we visit the mall.
            mall_benefit = sum(islice(tokens, m_i))
            
            # The net profit for visiting this mall is the benefit minus the travel/parking cost.
            net_profit = mall_benefit - cost
            
            # We only consider malls that result in a positive net profit.
            if net_profit > 0:
                yield net_profit

    # heapq.nlargest(k, ...) efficiently finds the k largest items from the generator.
    # This is an O(N log K) operation, providing a different approach to sorting the entire list.
    # If fewer than k positive profits exist, it correctly returns all of them.
    top_profits = heapq.nlargest(k, get_mall_net_profits())
    
    # The total maximum profit is the sum of these top-performing malls.
    # If no malls are profitable, the sum will naturally be 0.
    print(sum(top_profits))

if __name__ == "__main__":
    solve()

This editorial was generated by gemini-3-flash-preview.

投稿日時:
最終更新: