Skip to content

Commit 91028cc

Browse files
committed
CSES solutions
1 parent fec7629 commit 91028cc

27 files changed

+1035
-0
lines changed

CSES/DP/BookShop_MEMO.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using ld = long double;
6+
#define tcU template<class T
7+
tcU> using V = vector<T>;
8+
#define rep(i,a,b) for(int i=a;i<b;++i)
9+
#define per(i,a,b) for(int i=b;i>=a;i--)
10+
#define sz(a) (int)(a.size())
11+
#define all(a) a.begin(),a.end()
12+
#define precision(n) cout << fixed << setprecision(n)
13+
14+
void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
15+
const int mod = 1e9 + 7;
16+
tcU> T max(T &a,T b) {return a > b ? a : b;}
17+
tcU> T min(T &a, T b) { return a > b ? b : a;}
18+
19+
template<typename T1, typename T2> // cin >> pair
20+
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
21+
tcU> // cin >> vector
22+
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) { cin >> it; } return istream; }
23+
template<typename T1, typename T2> // cout << pair
24+
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
25+
tcU> // cout << vector
26+
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) { cout << it << " "; } return ostream; }
27+
28+
int f(int idx, int tar, vector<ll> &cost, vector<ll> &weight,vector<vector<ll>> &dp) {
29+
if (idx == 0) {
30+
if (tar >= cost[0]) return weight[0];
31+
return 0;
32+
}
33+
if (dp[idx][tar] != -1) return dp[idx][tar];
34+
int notake = f(idx-1,tar,cost,weight,dp);
35+
int take = 0;
36+
if (tar >= cost[idx]) take = f(idx-1,tar - cost[idx], cost, weight,dp) + weight[idx];
37+
return dp[idx][tar] = max(take , notake);
38+
}
39+
40+
void solve() {
41+
int n, x; cin >> n >> x;
42+
vector<ll> cost(n); cin >> cost;
43+
vector<ll> weight(n); cin >> weight;
44+
vector<vector<ll>> dp(n+1,vector<ll> (x+1,-1));
45+
cout << f(n-1,x,cost,weight,dp) << '\n';
46+
}
47+
48+
signed main() {
49+
ios_base::sync_with_stdio(false),cin.tie(nullptr);
50+
51+
int T = 1;
52+
rep(i,1,T+1){
53+
/* cout << "Case #" << i << ": "; */
54+
solve();
55+
}
56+
return 0;
57+
}
58+

CSES/DP/BookShop_TABU.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using ld = long double;
6+
#define tcU template<class T
7+
tcU> using V = vector<T>;
8+
#define rep(i,a,b) for(int i=a;i<b;++i)
9+
#define per(i,a,b) for(int i=b;i>=a;i--)
10+
#define sz(a) (int)(a.size())
11+
#define all(a) a.begin(),a.end()
12+
#define precision(n) cout << fixed << setprecision(n)
13+
14+
void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
15+
const int mod = 1e9 + 7;
16+
tcU> T max(T &a,T b) {return a > b ? a : b;}
17+
tcU> T min(T &a, T b) { return a > b ? b : a;}
18+
19+
template<typename T1, typename T2> // cin >> pair
20+
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
21+
tcU> // cin >> vector
22+
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) { cin >> it; } return istream; }
23+
template<typename T1, typename T2> // cout << pair
24+
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
25+
tcU> // cout << vector
26+
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) { cout << it << " "; } return ostream; }
27+
28+
void solve() {
29+
int n, x; cin >> n >> x;
30+
vector<int> cost(n); cin >> cost;
31+
vector<int> weight(n); cin >> weight;
32+
vector<vector<int>> dp(n+1,vector<int> (x+1,-1));
33+
34+
for (int i = 0; i <= x; ++i) {
35+
if (i >= cost[0]) {
36+
dp[0][i] = weight[0];
37+
} else dp[0][i] = 0;
38+
}
39+
40+
for (int i = 1; i < n; ++i) {
41+
for (int tar = 1; tar <=x; ++tar) {
42+
int notake = dp[i-1][tar];
43+
int take = 0;
44+
if (tar >= cost[i]) take = dp[i-1][tar-cost[i]] + weight[i];
45+
dp[i][tar] = max(take,notake);
46+
}
47+
}
48+
cout << dp[n-1][x];
49+
}
50+
51+
signed main() {
52+
ios_base::sync_with_stdio(false),cin.tie(nullptr);
53+
54+
int T = 1;
55+
rep(i,1,T+1){
56+
/* cout << "Case #" << i << ": "; */
57+
solve();
58+
}
59+
return 0;
60+
}
61+

CSES/DP/DiceCombination.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using ld = long double;
6+
#define tcU template<class T
7+
tcU> using V = vector<T>;
8+
#define rep(i,a,b) for(int i=a;i<b;++i)
9+
#define per(i,a,b) for(int i=b;i>=a;i--)
10+
#define sz(a) (int)(a.size())
11+
#define all(a) a.begin(),a.end()
12+
#define precision(n) cout << fixed << setprecision(n)
13+
#define nline '\n'
14+
15+
void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
16+
const int mod = 1e9 + 7;
17+
tcU> T max(T a,T b) {return a > b ? a : b;}
18+
tcU> T min(T a, T b) { return a > b ? b : a;}
19+
20+
template<typename T1, typename T2> // cin >> pair
21+
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
22+
tcU> // cin >> vector
23+
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) { cin >> it; } return istream; }
24+
template<typename T1, typename T2> // cout << pair
25+
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
26+
tcU> // cout << vector
27+
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) { cout << it << " "; } return ostream; }
28+
29+
void solve() {
30+
int n; cin >> n;
31+
vector<ll> dp(n+1,0);
32+
dp[0] = 1;
33+
dp[1] = 1;
34+
for (int i = 2; i <= n; ++i) {
35+
ll ways = 0;
36+
for (int k = 1; k <= 6; ++k) {
37+
if ((i-k) >= 0) {
38+
(ways += dp[i-k]) %= mod;
39+
}
40+
}
41+
(dp[i] = ways) %= mod;
42+
}
43+
cout << dp[n] % mod << '\n';
44+
}
45+
46+
signed main() {
47+
ios_base::sync_with_stdio(false),cin.tie(nullptr);
48+
49+
int T = 1;
50+
/* cin >> T; */
51+
rep(i,1,T+1){
52+
/* cout << "Case #" << i << ": "; */
53+
solve();
54+
}
55+
return 0;
56+
}

CSES/DP/GridPaths.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using ld = long double;
6+
#define tcU template<class T
7+
tcU> using V = vector<T>;
8+
#define rep(i,a,b) for(int i=a;i<b;++i)
9+
#define per(i,a,b) for(int i=b;i>=a;i--)
10+
#define sz(a) (int)(a.size())
11+
#define all(a) a.begin(),a.end()
12+
#define precision(n) cout << fixed << setprecision(n)
13+
14+
void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
15+
const int mod = 1e9 + 7;
16+
tcU> T max(T &a,T b) {return a > b ? a : b;}
17+
tcU> T min(T &a, T b) { return a > b ? b : a;}
18+
19+
template<typename T1, typename T2> // cin >> pair
20+
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
21+
tcU> // cin >> vector
22+
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) { cin >> it; } return istream; }
23+
template<typename T1, typename T2> // cout << pair
24+
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
25+
tcU> // cout << vector
26+
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) { cout << it << " "; } return ostream; }
27+
28+
int f(int i,int k,vector<vector<char>> &grid,vector<vector<int>> &dp) {
29+
if (i < 0 || k < 0 ) return 0;
30+
if (i == 0 && k == 0 && grid[i][k] != '*') return 1;
31+
32+
if (grid[i][k] == '*') return 0;
33+
if (dp[i][k] != -1) return dp[i][k];
34+
int one = f(i-1,k,grid,dp) % mod;
35+
int second = f(i,k-1,grid,dp) % mod;
36+
return (dp[i][k] = (one + second) %mod) %= mod;
37+
}
38+
39+
void solve() {
40+
int N; cin >> N;
41+
vector<vector<char>> grid(N,vector<char> (N));
42+
vector<vector<int>> dp(N+1,vector<int> (N+1,-1));
43+
rep(i,0,N) {
44+
rep(k,0,N) {
45+
cin >> grid[i][k];
46+
}
47+
}
48+
cout << f(N-1,N-1,grid,dp) << '\n';
49+
}
50+
51+
signed main() {
52+
ios_base::sync_with_stdio(false),cin.tie(nullptr);
53+
54+
int T = 1;
55+
rep(i,1,T+1){
56+
/* cout << "Case #" << i << ": "; */
57+
solve();
58+
}
59+
return 0;
60+
}
61+

CSES/DP/MinimizingCoins.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using ld = long double;
6+
#define tcU template<class T
7+
tcU> using V = vector<T>;
8+
#define rep(i,a,b) for(int i=a;i<b;++i)
9+
#define per(i,a,b) for(int i=b;i>=a;i--)
10+
#define sz(a) (int)(a.size())
11+
#define all(a) a.begin(),a.end()
12+
#define precision(n) cout << fixed << setprecision(n)
13+
#define nline '\n'
14+
15+
void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
16+
const int mod = 1e9 + 7;
17+
tcU> T max(T &a,T b) {return a > b ? a : b;}
18+
tcU> T min(T &a, T b) { return a > b ? b : a;}
19+
20+
template<typename T1, typename T2> // cin >> pair
21+
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
22+
tcU> // cin >> vector
23+
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) { cin >> it; } return istream; }
24+
template<typename T1, typename T2> // cout << pair
25+
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
26+
tcU> // cout << vector
27+
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) { cout << it << " "; } return ostream; }
28+
29+
void solve() {
30+
ll n, x; cin >> n >> x;
31+
vector<ll> coins(n);
32+
cin >> coins;
33+
34+
vector<ll>prev(x + 1,0);
35+
for (ll i = 0; i <= x; ++i) {
36+
if (i % coins[0] == 0 ) prev[i] = i / coins[0];
37+
else prev[i] = 1e9;
38+
}
39+
for (ll i = 1; i < n; ++i) {
40+
vector<ll > cur(x + 1, 0);
41+
for (ll tar = 1; tar <= x; ++tar) {
42+
ll take = 1e9;
43+
if (tar >= coins[i]) take = cur[tar-coins[i]] + 1;
44+
ll notake = prev[tar];
45+
cur[tar] = min(take,notake);
46+
}
47+
prev = cur;
48+
}
49+
cout << (prev[x] == 1e9 ? -1 : prev[x]) << '\n';
50+
}
51+
52+
signed main() {
53+
ios_base::sync_with_stdio(false),cin.tie(nullptr);
54+
55+
int T = 1;
56+
/* cin >> T; */
57+
rep(i,1,T+1){
58+
/* cout << "Case #" << i << ": "; */
59+
solve();
60+
}
61+
return 0;
62+
}
63+

CSES/DP/RemovingDigits.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
using ld = long double;
6+
#define tcU template<class T
7+
tcU> using V = vector<T>;
8+
#define rep(i,a,b) for(int i=a;i<b;++i)
9+
#define per(i,a,b) for(int i=b;i>=a;i--)
10+
#define sz(a) (int)(a.size())
11+
#define all(a) a.begin(),a.end()
12+
#define precision(n) cout << fixed << setprecision(n)
13+
14+
void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
15+
const int mod = 1e9 + 7;
16+
tcU> T max(T &a,T b) {return a > b ? a : b;}
17+
tcU> T min(T &a, T b) { return a > b ? b : a;}
18+
19+
int f(int N,vector<int> &dp) {
20+
if (N <= 0) return 0;
21+
22+
if (dp[N] != -1) return dp[N];
23+
24+
int ans = INT_MAX;
25+
int temp = N;
26+
while (temp != 0){
27+
int rem = temp % 10;
28+
if (rem != 0) {
29+
int ways = f(N-rem,dp) + 1;
30+
ans = min(ans,ways);
31+
}
32+
temp/=10;
33+
}
34+
return dp[N] = ans;
35+
}
36+
37+
void solve() {
38+
int N; cin >> N;
39+
vector<int> dp(N+1,-1);
40+
cout << f(N,dp) << std::endl;
41+
}
42+
43+
signed main() {
44+
ios_base::sync_with_stdio(false),cin.tie(nullptr);
45+
46+
int T = 1;
47+
rep(i,1,T+1){
48+
/* cout << "Case #" << i << ": "; */
49+
solve();
50+
}
51+
return 0;
52+
}
53+

0 commit comments

Comments
 (0)