Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
romankurnovskii committed Feb 24, 2023
1 parent 925be76 commit 06325d2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@ _site
.ruby-version
.jekyll-metadata
.vscode
.cph # codeforces helper

# Numerous always-ignore extensions
*.diff
Expand Down
12 changes: 12 additions & 0 deletions config/_default/languages.yaml
Expand Up @@ -90,6 +90,18 @@ en:
identifier: algorithms-101
url: /en/tracks/algorithms-101/
parent: roadmaps
- name: Data Structures
identifier: algorithms-101-data-structures
url: /en/tracks/algorithms-101/data-structures
parent: algorithms-101
- name: Codeforces
identifier: algorithms-101-codeforces
url: /en/tracks/algorithms-101/codeforces
parent: algorithms-101
- name: LeetCode
identifier: algorithms-101-problems
url: /en/tracks/algorithms-101/problems
parent: algorithms-101
- name: AWS Developer Associate
identifier: aws-developer-roadmaps
url: /en/tracks/aws-certified-developer-associate/
Expand Down
10 changes: 5 additions & 5 deletions config/_default/menus.yaml
Expand Up @@ -21,6 +21,11 @@ main:
url: /categories/programming/
parent: notes
weight: 202
- name: Алгоритмы | EN
identifier: algorithms
url: ../en/tracks/algorithms-101/
parent: docslist
# weight: 202
- name: Links
identifier: links
url: ./../en/p/links/
Expand All @@ -43,11 +48,6 @@ main:
url: /tracks/disser/
parent: docslist
weight: 213
- name: Algorithms [EN]
identifier: algorithms
url: ./../en/tracks/algorithms-101/
parent: docslist
weight: 210

# Notes -> Programming ->
- name: Algorithms [EN]
Expand Down
2 changes: 1 addition & 1 deletion content/posts/bash-snippets.en.md
@@ -1,7 +1,7 @@
---
title: Bash code snippets
seoTitle: Bash code snippets
description: Bash code snippets
description: This bash code snippet provides a useful solution for renaming files in the current directory based on a specific pattern. The script loops through all files in the directory and checks if the filename contains the specified pattern (in this case, the "β„–" symbol). If it does, the script removes everything before and including the symbol and renames the file with the new name. The script then prints a message for each renamed file. This code snippet can be a useful time-saver for those who need to rename many files based on a certain pattern.
toc: true
categories: [Linux, programming, Bash, CheatSheet]
series: [CheatSheet]
Expand Down
57 changes: 52 additions & 5 deletions content/tracks/algorithms-101/codeforces/849-div-4-1791.en.md
Expand Up @@ -449,13 +449,18 @@ https://codeforces.com/contest/1791/problem/G1
**Statement:**

You are playing a game where you are given a **list** of **teleporters** (`0,2,…,𝑛`), each located at a point on the number line. The number line includes all integers from `0` to `n`. At point `i`, you can do one of three actions:
You are playing a game where you are given a **list** of **teleporters** (`0,2,…,𝑛`), each located at a point on the number line. The number line includes all integers from `0` to `n`.

- Move left one unit: this action costs `1` coin.
- Move right one unit: this action also costs `1` coin.
At point `i`, you can do one of three actions:
- Move left **one** unit: this action costs `1` coin.
- Move right **one** unit: this action also costs `1` coin.
- Use a teleporter at point `i`: this action costs $a_i$ coins. When you use a teleporter, you are immediately teleported back to point `0`.

You start at point `0`, and you have c coins to spend. Your goal is to use as many teleporters as possible while still having at least 1 coin left over. You cannot use a teleporter more than once.
> Last statement in the problem means that at any point i on the number line, you have the option to use a teleporter located at that point, which will immediately transport you back to point `0`. However, using a teleporter costs a certain number of coins, denoted by $a_i$. For example, if you are at point `i = 5`, and you use the teleporter located at point `5`, you will be immediately transported back to point `0`, but you will have to pay the cost $a_i$ in coins to do so.
>
> Essentially, the teleporters provide a way for you to quickly move back to the starting point (point `0`) without having to take multiple steps, but this convenience comes at a cost. You can only use each teleporter once, and you must have enough coins to pay the cost of using it.
You start at point `0`, and you have `c` coins to spend. Your goal is to use as many teleporters as possible while still having at least 1 coin left over. You cannot use a teleporter more than once.

Write a function `max_teleporters(n, c, a)` that takes in
- the length of the number line `n`
Expand All @@ -478,6 +483,32 @@ max_teleporters(n, c, a) => 2

Will use [greedy](/en/tags/greedy/) algorithm.

**Approach:**

Given input: `n = 8`, `c = 32`, `a = [100, 52, 13, 6, 9, 4, 100, 35]`

**Step 1:** Calculate the cost of each teleporter

- For each teleporter at position i, add the index i and the cost a[i]
- The resulting array cost contains the costs to use each teleporter, accounting for the cost of moving to that teleporter's position: [101, 54, 16, 10, 14, 10, 107, 43]

**Step 2:** Sort the cost array in ascending order

- Sorting the array ensures that we use the cheapest teleporters first

**Step 3:** Compute the maximum number of teleporters we can use

- Initialize a variable used to 0 to keep track of how many teleporters we've used
- Iterate over the sorted cost array, adding the cost of each teleporter to a running total total
- If the current total is less than or equal to c, we can use the current teleporter, so increment used
- If the current total is greater than c, we've run out of coins and can't use any more teleporters, so exit the loop
- Return the final value of used

**Step 4:** Return the maximum number of teleporters we can use

- The maximum number of teleporters we can use is the value of used computed in **Step 3**: `2`


**Solution:**

```python
Expand Down Expand Up @@ -508,11 +539,27 @@ def max_teleporters(n, c, a):

**Explanation from Codeforces:**

It's easy to see that it's optimal to only move right or to use a portal once we are at it. We can notice that when we teleport back, the problem is independent of the previous choices. We still are at point `0` and have some portals left. Thus, we can just find out the individual cost of each portal, sort portals by individual costs, and take them from smallest to largest by cost as long as we can. The cost of portal `𝑖` is `𝑖+π‘Žπ‘–` (since we pay `π‘Žπ‘–` to use it and need `𝑖` moves to get to it).
It's easy to see that it's optimal to only move right or to use a portal once we are at it. We can notice that when we teleport back, the problem is independent of the previous choices.

We still are at point `0` and have some portals left. Thus, we can just find out the individual cost of each portal, sort portals by individual costs, and take them from smallest to largest by cost as long as we can.

The cost of portal `𝑖` is $𝑖+π‘Žπ‘–_$ (since we pay &π‘Ž_𝑖& to use it and need `𝑖` moves to get to it).


### TODO G2. Teleporters (Hard Version)

https://codeforces.com/contest/1791/problem/G2

> binary, search, greedy, sortings *1900


**Explanation from Codeforces:**

Please also refer to the tutorial for the easy version.

If we are not at the first taken portal, the problem is still independent for each portal, but this time the cost of a portal is $π‘šπ‘–π‘›(π‘Ž_𝑖+𝑖,π‘Ž_𝑖+𝑛+1βˆ’π‘–)$ (since we can come to a portal either from point 0 or point $𝑛+1$).

So, we again sort the portals by their costs. But this time, we need to make sure that the first taken portal is taken from point `0`, so we will iterate over all portals and check the maximum amount of portals we can take if we use it as the first one.

We can check this using prefix sums over the minimum cost array and binary searching, checking if the amount of considered portals taken doesn't exceed the number of coins we initially have (we also have to deal with the case when the portal we are considering is included both times as the initial portal and in the minimum cost prefix).
4 changes: 3 additions & 1 deletion content/tracks/algorithms-101/codeforces/_index.en.md
Expand Up @@ -12,7 +12,9 @@ draft: false
weight: 40
---

[Python template for contests](/en/tracks/algorithms-101/helpers/#python-template-for-contests)
- [Python template for contests](./cp-template)
- [Competitive Programming Helper (cph) | VSCode extension](https://marketplace.visualstudio.com/items?itemName=DivyanshuAgrawal.competitive-programming-helper)
- [Competitive Programming | browser extension](https://github.com/jmerle/competitive-companion)


## Links
Expand Down

0 comments on commit 06325d2

Please sign in to comment.