Competitive Programming Tasks 2
Competitive Programming Tasks 2
Competitive Programming Tasks 2
Here are a few example tasks from the International Olympiad in Informatics (IOI) style, along with brief
solutions:
**Problem**: Given a directed graph with weighted edges, find the shortest path from a starting node
to a target node.
**Input**:
- The first line contains two integers, \( n \) (number of nodes) and \( m \) (number of edges).
- The next \( m \) lines each contain three integers \( u \), \( v \), and \( w \) (an edge from \( u \) to \( v \)
with weight \( w \)).
- The last line contains two integers, \( s \) (start node) and \( t \) (target node).
**Output**: The length of the shortest path from \( s \) to \( t \), or "NO PATH" if no path exists.
**Solution**:
```python
import heapq
for u, v, w in edges:
graph[u].append((v, w))
dist = [float('inf')] * n
dist[start] = 0
while priority_queue:
continue
dist[neighbor] = distance
# Example usage:
n=5
edges = [(0, 1, 10), (0, 2, 5), (1, 2, 2), (1, 3, 1), (2, 1, 3), (2, 3, 9), (2, 4, 2), (4, 3, 6)]
start, end = 0, 3
```
**Problem**: Given a list of items, each with a weight and a value, determine the maximum value that
can be obtained with a knapsack of capacity \( W \).
**Input**:
- The first line contains two integers, \( n \) (number of items) and \( W \) (maximum weight).
- The next \( n \) lines each contain two integers, \( w_i \) (weight) and \( v_i \) (value).
**Solution**:
```python
dp = [0] * (W + 1)
return dp[W]
# Example usage:
n=3
W=5
print(knapsack(n, W, items))
```
**Input**:
**Solution**:
```python
inv_count = 0
temp_arr[k] = arr[i]
i += 1
else:
temp_arr[k] = arr[j]
j += 1
k += 1
while i <= mid:
temp_arr[k] = arr[i]
i += 1
k += 1
temp_arr[k] = arr[j]
j += 1
k += 1
arr[i] = temp_arr[i]
return inv_count
inv_count = 0
return inv_count
# Example usage:
n = len(arr)
temp_arr = [0] * n
```
These examples illustrate various common algorithmic problems. If you need more tasks or different
types, feel free to ask!
Here are some additional tasks that resemble those you might find in the International Olympiad in
Informatics (IOI), along with their solutions:
**Problem**: Given an array of integers, find the length of the longest increasing subsequence.
**Input**:
**Solution**:
```python
import bisect
def longest_increasing_subsequence(arr):
lis = []
for x in arr:
pos = bisect.bisect_left(lis, x)
if pos == len(lis):
lis.append(x)
else:
lis[pos] = x
return len(lis)
# Example usage:
print(longest_increasing_subsequence(arr))
```
**Problem**: Given an array of integers, find the maximum sum of a contiguous subarray.
**Input**:
**Solution**:
```python
def max_subarray_sum(arr):
for x in arr[1:]:
max_ending_here = max(x, max_ending_here + x)
return max_so_far
# Example usage:
print(max_subarray_sum(arr))
```
**Problem**: Given a list of coin denominations and a total amount, determine the number of ways to
make change for that amount using the coins.
**Input**:
- The first line contains two integers, \( n \) (number of denominations) and \( amount \) (the total
amount).
**Solution**:
```python
dp = [0] * (amount + 1)
return dp[amount]
# Example usage:
denominations = [1, 2, 5]
amount = 5
print(coin_change(denominations, amount))
```
**Problem**: Given weights and values of \( n \) items, and a maximum capacity \( W \), determine the
maximum value that can be put in the knapsack.
**Input**:
- The next \( n \) lines contain two integers each: \( w_i \) (weight) and \( v_i \) (value).
**Output**: The maximum value achievable with the given weights and values.
**Solution**:
```python
if weight <= w:
else:
return dp[n][W]
# Example usage:
n=3
W=4
print(knapsack_01(n, W, items))
```
**Input**:
**Solution**:
Use backtracking to generate permutations.
```python
def permute(s):
result = []
if not options:
result.append(''.join(path))
return
for i in range(len(options)):
backtrack([], list(s))
return sorted(result)
# Example usage:
s = "abc"
print(permute(s))
```
These tasks demonstrate a variety of algorithmic concepts and approaches. If you’d like more examples
or specific types of tasks, let me know!