Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Katas/Algorithms/greedy/Greedy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Greedy

Greedy is an algorithmic paradigm that builds a problem piece by piece, choosing in that moment the next piece that seems more efficient.

Don't use the divide and conquer; we go right away to the answer, and we make the best decision at the moment. Regardless of the future, don't be the best.

###### _Example:_

- [Coin Example](./example/src/solution.cpp)

###### **Katas:**

- [2276A Perfect Sandwiches](../../CP/perfect_sanduches/src/solution.cpp)
28 changes: 28 additions & 0 deletions Katas/Algorithms/greedy/example/src/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
int main() {

int n;
int amount;
std::cin >> n >> amount;
int coins[n];
int rest = 0;
for (int i = 0; i < n; i++) {
std::cin >> coins[i];
}

for (int i = n - 1; i >= 0; i--) {
if (amount >= coins[i]) {
int cnt = (amount / coins[i]);
rest += cnt;
amount -= (cnt * coins[i]);
}

if (amount == 0) {
break;
}
}

std::cout << rest << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions Katas/Algorithms/greedy/example/test/expected1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
2 changes: 2 additions & 0 deletions Katas/Algorithms/greedy/example/test/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3 20
1 10 18
35 changes: 35 additions & 0 deletions Katas/CP/nexround/src/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);

int n, k;
std::cin >> n >> k;
int scores[n];

for (int i = 0; i < n; i++) {
std::cin >> scores[i];
}

int count = 0;
for (int i = 0; i < n; i++) {
if (scores[i] == 0)
break;
else {
if (scores[i] > 0 && i + 1 <= k) {
count++;
} else {
if (scores[i] == scores[k - 1]) {
count++;
} else {
break;
}
}
}
}
std::cout << count << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions Katas/CP/nexround/test/expected1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
2 changes: 2 additions & 0 deletions Katas/CP/nexround/test/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8 5
10 9 8 7 7 7 5 5
42 changes: 42 additions & 0 deletions Katas/CP/sauSaGebank/src/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// #include <cmath>
// #include <iostream>
// int main() {
// int t, n, k;
// int a = 1;
// int c = 0;
//
// std::cin >> t;
// for (int i = 0; i < t; i++) {
// std::cin >> n >> k;
// for (int j = n; j > 0; j--) {
// if (j >= k) {
// if (j == (n - k)) {
// c += a;
// a = 1;
// k--;
// } else {
// a = pow(2, j);
// }
// }
// }
// std::cout << c << std::endl;
// }
//
// return 0;
// }

#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int tc;
cin >> tc;
while (tc--) {
int n, k;
cin >> n >> k;
cout << (1 << (n - k + 1)) + 2 * (k - 1) << '\n';
}
return 0;
}
Binary file added Katas/CP/sauSaGebank/src/solutionv2
Binary file not shown.
5 changes: 5 additions & 0 deletions Katas/CP/sauSaGebank/test/expected1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2
4
8
10
514
6 changes: 6 additions & 0 deletions Katas/CP/sauSaGebank/test/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
1 1
2 1
4 3
5 5
10 2
8 changes: 7 additions & 1 deletion Katas/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# 2 codeforces and contest
# Code forces

I share my logic and my mistakes

### TOPICS

- [Greedy](./Algorithms/greedy/Greedy.md)
Loading