Skip to content

CSES Problemset #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions cses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CSES is one of the most complete problemsets on the internet, with over 300 problems of all topics.
https://cses.fi/problemset/list/

In here we have all solutions to the problems of the dp and graph sections

All the problems can be tested on the cses website, where you can also access the test cases, and once you solved the problem, access other solutions
40 changes: 40 additions & 0 deletions cses/dp/1 - Dice Combinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Dice Combinations
//
// Problem name: Dice Combinations
// Problem Link: https://cses.fi/problemset/task/1633
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;
int dp[1000005];

int dice(int n) {
int resp = 0;

// caso base
if (n == 0) return 1;

// se ja calculamos a dp
if (dp[n] > 0) {
return dp[n];
}
for (int i = 1; i<= 6; i++) {
if (n - i >= 0) {
resp += dice(n-i);

// lembrar de tirar o modulo
resp %= mod;
}
}
return dp[n] = resp;
}

int main () {
int n;
cin >> n;
cout << dice(n) << endl;
return 0;
}
43 changes: 43 additions & 0 deletions cses/dp/10 - Edit Distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Edit Distance
//
// Problem name: Edit Distance
// Problem Link: https://cses.fi/problemset/task/1639
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

// The edit distance between two strings is the minimum number of operations required to transform one string into the other.
// The allowed operations are:
// Add one character to the string.
// Remove one character from the string.
// Replace one character in the string.
// For example, the edit distance between LOVE and MOVIE is 2, because you can first replace L with M, and then add I.
// Your task is to calculate the edit distance between two strings.

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int main () { _
string a, b;
cin >> a >> b;
int tama = (int)a.size(), tamb = (int)b.size();
vector<vector<int>> dp(5050, vector<int> (5050, 10000));
dp[tama][tamb] = 0;
for (int i = tama; i >= 0; i--) {
for (int j = tamb; j >= 0; j--) {
dp[i][j] = min(dp[i][j], (a[i] != b[j]) + dp[i+1][j+1]);
dp[i][j] = min(dp[i][j], 1 + dp[i+1][j]);
dp[i][j] = min(dp[i][j], 1 + dp[i][j+1]);
}
}
cout << dp[0][0] << '\n';
return 0;
}
41 changes: 41 additions & 0 deletions cses/dp/11 - Rectangle Cutting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Rectangle Cutting
//
// Problem name: Rectangle Cutting
// Problem Link: https://cses.fi/problemset/task/1744
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

// Given an a×b rectangle, your task is to cut it into squares.
// On each move you can select a rectangle and cut it into two rectangles
// in such a way that all side lengths remain integers. What is the minimum possible number of moves?

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int main () { _
int a, b;
cin >> a >> b;
vector<vector<int>> dp(505, vector<int> (505, INF));
for (int i = 1; i <= a; i++) {
dp[i][i] = 0;
for (int j = 1; j <= b; j++) {
for (int k = 1; k < j; k++) {
dp[i][j] = min(dp[i][j], 1 + dp[i][j-k] + dp[i][k]);
}
for (int k = 1; k < i; k++) {
dp[i][j] = min(dp[i][j], 1 + dp[i-k][j] + dp[k][j]);
}
}
}
cout << dp[a][b] << '\n';
return 0;
}
41 changes: 41 additions & 0 deletions cses/dp/12 - Money Sums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Money Sums
//
// Problem name: Money Sums
// Problem Link: https://cses.fi/problemset/task/1745
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int main () { _
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
vector<int> dp(1e5 + 5);
dp[0] = 1;
int c = 0;
for (int i = 0; i < n; i++) {
for (int j = 1e5; j >= 0; j--) {
if (dp[j] && j+v[i] <= 1e5) {
c += (dp[j+v[i]] == 0);
dp[j+v[i]] = 1;
}
}
}
cout << c << '\n';
for (int i = 1; i <= 1e5; i++)
if (dp[i]) cout << i << ' ';
cout << '\n';
return 0;
}
40 changes: 40 additions & 0 deletions cses/dp/13 - Removal game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Removal Game
//
// Problem name: Removal Game
// Problem Link: https://cses.fi/problemset/task/1097
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int main () { _
int n;
cin >> n;
vector<int> v(n);
vector<vector<ll>> dp(n, vector<ll> (n));
ll soma = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
soma += v[i];
dp[i][i] = v[i];
}
for (int j = 1; j < n; j++) {
for (int i = 0; i < n; i++) {
if (i + j < n) {
dp[i][i+j] = max(v[i] - dp[i+1][i+j], v[i+j] - dp[i][i+j-1]);
}
}
}
cout << (soma+dp[0][n-1])/2 << '\n';
return 0;
}
49 changes: 49 additions & 0 deletions cses/dp/14 - Two Sets II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Two Sets II
//
// Problem name: Two Sets II
// Problem Link: https://cses.fi/problemset/task/1093
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int fexp(ll b, int e) {
ll resp = 1;
while (e) {
if (e&1) resp = (resp * b) % M;
b = (b * b) % M;
e = (e >> 1);
}
return resp;
}

int main () { _
int n;
cin >> n;
if ((n%4) && ((n+1)%4)) cout << "0\n";
else {
int meta = n*(n+1)/4;
vector<ll> dp(meta+1);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = meta; j >= 0; j--) {
if (j+i <= meta) {
dp[j+i] += dp[j];
if (dp[j+i] >= M) dp[j+i] -= M;
}
}
}
cout << (dp[meta] * fexp(2, M-2)) % M << '\n';
}
return 0;
}
52 changes: 52 additions & 0 deletions cses/dp/15 - Projects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Projects
//
// Problem name: Projects
// Problem Link: https://cses.fi/problemset/task/1140
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int main () { _
int n;
cin >> n;
vector<pair<pii, int>> v(n);
vector<int> c;
for (int i = 0; i < n; i++) {
cin >> v[i].F.F >> v[i].F.S >> v[i].S;
c.pb(v[i].F.F), c.pb(v[i].F.S);
}
sort(c.begin(), c.end());
map<int, int> mapa;
int atual = 0;
mapa[c[0]] = atual++;
for (int i = 1; i < (int)c.size(); i++) {
if (c[i] != c[i-1]) mapa[c[i]] = atual++;
}
for (int i = 0; i < n; i++) {
v[i].F.F = mapa[v[i].F.F];
v[i].F.S = mapa[v[i].F.S];
}
sort(v.begin(), v.end());
vector<ll> dp(1e6);
atual = n-1;
for (int i = 5e5; i >= 0; i--) {
dp[i] = max(dp[i], dp[i+1]);
while (atual >= 0 && i == v[atual].F.F) {
dp[i] = max(dp[i], v[atual].S + dp[v[atual].F.S + 1]);
atual--;
}
}
cout << dp[0] << '\n';
return 0;
}
40 changes: 40 additions & 0 deletions cses/dp/16 - Increasing Subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Increasing Subsequence
//
// Problem name: Increasing Subsequence
// Problem Link: https://cses.fi/problemset/task/1145
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)

// You are given an array containing n integers. Your task is to determine the longest
//increasing subsequence in the array, i.e.,
// the longest subsequence where every element is larger than the previous one.
// A subsequence is a sequence that can be derived from the array by deleting
// some elements without changing the order of the remaining elements.

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 1000100
#define INF 100000000
#define pb push_back
#define F first
#define S second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;

int main () { _
int n;
cin >> n;
vector<int> v(n), pilha;
for (int i = 0; i < n; i++) {
cin >> v[i];
auto it = lower_bound(pilha.begin(), pilha.end(), v[i]);
if (it == pilha.end()) {
pilha.pb(v[i]);
}
else *it = v[i];
}
cout << (int)pilha.size() << '\n';
return 0;
}
Loading