Problem 3 - Shopping Spree

Here, we will discuss the solution to Shopping Spree from AIO 2024.

Summary

Pair Mart sells N items where N is even, and you want to buy all of them. At Pair Mart, you can only buy items in pairs, and you pay the cost of the more expensive item. Additionally, you have K coupons, which allow you to buy two items for the cost of the cheaper item.

What is the minimum cost to buy all N items?

Note that the prices are given in ascending order, meaning that item 1 with a cost of C1 is the cheapest item and item N with a cost of CN is the most expensive item.

Solution

Subtask 1: K = 0 and N ≤ 1000

The crux of this subtask is deciding how to optimally pair up the items when we have no coupons.

Consider the most expensive item (item N):

Using a similar argument, it is best to pair the third most expensive item with the fourth most expensive item and continue this until all the items have been paired up. This gives us a greedy algorithm that successfully solves this subtask.

Consider an example with N = 8, K = 0, and items with prices 1, 2, 3, 4, 5, 6, 7, 8:

This algorithm has a time complexity of O(N), however, slower algorithms with a time complexity of O(N2) will also pass this subtask.

Subtask 2: K = N/2 and N ≤ 1000

The crux of this subtask is deciding how to optimally pair up the items when we have enough coupons to use on every pair.

We will use a greedy algorithm, similar to subtask 1. This time, consider the cheapest item (item 1):

Using a similar argument, it is best to pair the second cheapest item with the second most expensive item and continue this until all the items have been paired up.

Consider an example with N = 8, K = 4, and items with prices 1, 2, 3, 4, 5, 6, 7, 8:

This algorithm has a time complexity of O(N), however, slower algorithms with a time complexity of O(N2) will also pass this subtask.

Full Solution

We can combine our solutions for the first two subtasks:

Consider an example with N = 8, K = 2, and items with prices 1, 2, 3, 4, 5, 6, 7, 8:

This algorithm has a time complexity of O(N) which is fast enough to score 100 in this problem.

Code

Below you can see the full solution written in Python and C++. I suggest trying to code the solution yourself, look at our code after you finish or if you get stuck.

# Open the input and output files.
input_file = open("shopin.txt", "r")
output_file = open("shopout.txt", "w")

# Read the value of N and the string of characters from the input file.
input_line = input_file.readline().strip()
N, K = map(int, input_line.split())
input_line = input_file.readline().strip()
costs = list(map(int, input_line.split()))

answer = 0

# First, use the coupons to buy the K cheapest and most expensive items
# We just pay the cost of the cheapest K items
for i in range(K):
   answer += costs[i]

# We now buy the remaining items, paying the cost of every second item
for i in range(K+1, N-K, 2):
   answer += costs[i]

# Write the answer to the output file.
output_file.write("%d\n" % (answer))

# Finally, close the input/output files.
input_file.close()
output_file.close()
#include <cstdio>
int N, K, costs[200005];
int main(void) {
   /* Open the input and output files. */
   FILE *input_file = fopen("shopin.txt", "r");
   FILE *output_file = fopen("shopout.txt", "w");

   /* Read the value of N and the string of characters from the input file. */
   fscanf(input_file, "%d%d", &N, &K);
   for (int i = 0; i < N; i++) {
       fscanf(input_file, "%d", &costs[i]);
   }

   int answer = 0;

   // First, use the coupons to buy the K cheapest and most expensive items
   // We just pay the cost of the cheapest K items
   for (int i = 0; i < K; i++) {
       answer += costs[i];
   }

   // We now buy the remaining items, paying the cost of every second item
   for (int i = K+1; i < N-K; i += 2) {
       answer += costs[i];
   }

   /* Write the answer to the output file. */
   fprintf(output_file, "%d\n", answer);

   /* Finally, close the input/output files. */
   fclose(input_file);
   fclose(output_file);
}

Click here to go back.