diff --git a/competitive_programming/abctripletatc.cpp b/competitive_programming/abctripletatc.cpp new file mode 100644 index 0000000..bb6b61e --- /dev/null +++ b/competitive_programming/abctripletatc.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +int main () +{ + long long n; + cin >> n; + long long a, b, c, triplets = 0; + for ( a = 1; a <= n; a++) + { + for ( b = a; b <= n; b++) + { + for ( c = b; c <= n; c++) + { + if (a*b*c <= n) triplets++; + } + + } + + } + + cout << triplets << endl; + +} diff --git a/competitive_programming/ancientcivicf.cpp b/competitive_programming/ancientcivicf.cpp new file mode 100644 index 0000000..f877eed --- /dev/null +++ b/competitive_programming/ancientcivicf.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +int main() +{ + int t; + cin >> t; + while (t--) + { + int n, l, i = 0, j = 0; + cin >> n >> l; + int deci[n]; + while (n--) + { + + cin >> deci[i]; + i++; + } + + int bina[n][l]; + for (i = 0; i < n; i++) + { + for (j = 0; j < l; j++) + { + bina[i][j] = 0; + } + } + for (i = 0; i < n; i++) + { + + for (j = l-1; j >= 0; j--) + { + if (deci[n]) + { + bina[i][j] = deci[n] % 2; + deci[n] = deci[n] / 2; + } + cout << bina[i][j]; + } + cout << endl; + } + } +} diff --git a/competitive_programming/average.cpp b/competitive_programming/average.cpp new file mode 100644 index 0000000..bfc1d96 --- /dev/null +++ b/competitive_programming/average.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; +int main() +{ + int t, n, c = 0; + cin >> t; + while (t--) + { + float outs = 0, runs = 0; + float ans; + c++; + cin >> n; + while (n--) + { + string run; + cin >> run; + int x = run.size() - 1; + if (run[x] != '*') + outs++; + float run2; + stringstream ss; + ss << run; + ss >> run2; + runs += run2; + } + if (outs == 0) + cout << "Case " << c << ":" << " " << -1 << endl; + else + { + ans = runs / outs; + if (ans - int(ans) > 0) + { + cout << "Case " << c << ":" << " " << -1 << endl; + } + else + cout << "Case " << c << ":" << " " << int(ans) << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/competitive_programming/buildingareaguess.cpp b/competitive_programming/buildingareaguess.cpp new file mode 100644 index 0000000..4d4f5ef --- /dev/null +++ b/competitive_programming/buildingareaguess.cpp @@ -0,0 +1,25 @@ +//ac +#include +using namespace std; +int main() +{ + int n, i, rightguess = 0; + cin >> n; + int s, b, zerornot; + for (i = 0; i < n; i++) + { + cin >> s; + + for (b = 1; b <= (s - 3) / 7; b++) // for a = 1 we get the highest number of b + { + zerornot = (s - 3 * b) % (4 * b + 3); // determining --> is it a natural number or fractional + if (zerornot == 0) + { + rightguess++; + break; + } + } + } + cout << n - rightguess << endl; + return 0; +} \ No newline at end of file diff --git a/competitive_programming/cellguesscf.cpp b/competitive_programming/cellguesscf.cpp new file mode 100644 index 0000000..eb3ef50 --- /dev/null +++ b/competitive_programming/cellguesscf.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() { + long long t , n, m; + cin >> t; + for(int i = 0 ; i < t ; i++){ + cin >> n >> m; + if (n==m==1) + cout << 0 << endl; + else if (n +#include +int i; +using namespace std; +int frac(int x, int y) +{ + int z; + z = x / y; + cout << z << " "; + int rem; + rem = x%y; + if(rem!=1) + frac(y, rem); + else + cout << y; + return 0; +} + +int howmuch(int x, int y) +{ + i++; + int rem; + rem = x%y; + if(rem!=1) + howmuch(y, rem); + else + cout << i << " "; + return 0; +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + i = 0; + int x, y, rem; + cin >> x >> y; + howmuch(x,y); + frac(x, y); + cout << endl; + } +} diff --git a/competitive_programming/daily_star.py b/competitive_programming/daily_star.py new file mode 100644 index 0000000..77ce0bd --- /dev/null +++ b/competitive_programming/daily_star.py @@ -0,0 +1,9 @@ +import requests +from bs4 import BeautifulSoup +daily_star = requests.get('https://www.thedailystar.net') +parsed_html = BeautifulSoup(daily_star.text, 'html.parser') +headlines = parsed_html.find_all('h3') +for i in headlines: + if len(i.text.strip()) > 0: + print(i.text) + \ No newline at end of file diff --git a/competitive_programming/deviationcf.cpp b/competitive_programming/deviationcf.cpp new file mode 100644 index 0000000..d0e8571 --- /dev/null +++ b/competitive_programming/deviationcf.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +int main() +{ + int t, x = 6; + cin >> t; + int a[3]; + for (int i = 0; i < t; i++) + { + + cin >> a[0] >> a[1] >> a[2]; + while (x--) + { + if (a[0] + a[2] - 2 * a[1] == 0) + { + cout << 0 << endl; + break; + } + else if (abs(a[0] + a[2] - 2 * a[1]) == 1) + { + cout << 1 << endl; + break; + } + else + { + if (a[0] + a[2] - 2 * a[1] > 1) + { + a[1] = a[1] + 1; + a[0] = a[0] - 1; + } + else + { + a[1] = a[1] - 1; + a[0] = a[0] + 1; + } + } + } + } +} \ No newline at end of file diff --git a/competitive_programming/distancemanhattancf.cpp b/competitive_programming/distancemanhattancf.cpp new file mode 100644 index 0000000..b66b24e --- /dev/null +++ b/competitive_programming/distancemanhattancf.cpp @@ -0,0 +1,26 @@ +#include +#include + +int main() { + float t, x, y, a, b, d_bcac; + std::cin >> t; + for (int i=0; i> x >> y; + d_bcac = (x+y)/2; //if abs used then d_bcac won't be a float number + if(d_bcac - round(d_bcac) == 0) + { + for (int i=0; i<=d_bcac; i++) + { + a=i; + b=d_bcac-i; + if (abs(a-x) +abs(b-y) == d_bcac) + break; + } + std::cout << a << " " << b << "\n"; + } + //std::cout << (x+2*d_bcac-y)/2 << " " << (y+2*d_bcac-x)/2 << "\n"; + else + std::cout << -1 << " " << -1 << "\n"; + } +} \ No newline at end of file diff --git a/competitive_programming/divanstorecf.cpp b/competitive_programming/divanstorecf.cpp new file mode 100644 index 0000000..f269c0d --- /dev/null +++ b/competitive_programming/divanstorecf.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int main() { + int t, i; + cin >> t; + for(i = 0 ; i < t ; i++){ + int n, l, r, k, a = 0, spended =0, chocolates = 0; + cin >> n >> l >> r >> k; + int chocoprice[n]; + for (i = 0; i < n; i++) cin >> chocoprice[i]; + for ( i = 0; i < n; i++) + { + if(chocoprice[i] >= l && chocoprice[i] <= r) a++; + } + int buyable[a]; + for (int i = 0; i < n; i++) + { + if(chocoprice[i] >= l && chocoprice[i] <= r) buyable[a] = chocoprice[i]; + } + int asize = sizeof(buyable) / sizeof(buyable[0]); + sort(buyable, buyable + asize); + for ( i = 0; i < a; i++) + { + spended += buyable[i]; + if (spended <= k) chocolates++; + } + cout << chocolates << endl; + } + return 0; +} \ No newline at end of file diff --git a/competitive_programming/divmulcf.cpp b/competitive_programming/divmulcf.cpp new file mode 100644 index 0000000..e838ee0 --- /dev/null +++ b/competitive_programming/divmulcf.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +int main() +{ + int t, i, j; + cin >> t; + for (j = 0; j < t; j++) + { + int n; + cin >> n; + int a[n], odd[n], mult2 = 0, highestodd = 1, highestoddindex = 0, highwithodd, highmult = 0, power; + long long sum; + if (n == 1) + { + cin >> a[0]; + cout << a[0] << endl; + } + else + { + for (i = 0; i < n; i++) + { + cin >> a[i]; + odd[i] = a[i]; + while (1) + { + if (odd[i] % 2 == 1) + break; + else + { + odd[i] = odd[i] / 2; + mult2++; + } + } + if (odd[i] > highestodd) + { + highestodd = odd[i]; + highestoddindex = i; + } + } + highwithodd = a[highestoddindex]; + while (1) + { + if (highwithodd % 2 == 1) + break; + else + { + highwithodd = highwithodd / 2; + highmult++; + } + } + power = mult2 - highmult; + sum = a[highestoddindex] * pow(2, power); + for (i = 0; i < n; i++) + { + if (highestoddindex != i) + sum += odd[i]; + } + cout << sum << endl; + } + } + return 0; +} diff --git a/competitive_programming/dumb.cpp b/competitive_programming/dumb.cpp new file mode 100644 index 0000000..475948a --- /dev/null +++ b/competitive_programming/dumb.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; +int main() +{ + int t, n; + cin >> t; + float ans = 1, zero = 0; + while (t--) + { + cin >> n; + if(n != 0) + cout << fixed << setprecision(5) << ans << endl; + else + cout << fixed << setprecision(5) << zero << endl; + } + +} \ No newline at end of file diff --git a/competitive_programming/dumb.py b/competitive_programming/dumb.py new file mode 100644 index 0000000..eab5c45 --- /dev/null +++ b/competitive_programming/dumb.py @@ -0,0 +1,7 @@ +t = int(input()) +c = 1 +for i in range (t): + a = float(input()) + b = float(input()) + print(f"Case {c}: {1/(1/a - 1/b)}") + c += 1 diff --git a/competitive_programming/equalornotcf.cpp b/competitive_programming/equalornotcf.cpp new file mode 100644 index 0000000..d88b26a --- /dev/null +++ b/competitive_programming/equalornotcf.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +int main() +{ + int t; + cin >> t; + string s; + while (t--) + { + cin >> s; + if (s[0] == s[s.size() - 1]) + { + cout << "YES" << endl; + } + else + cout << "NO" << endl; + + } + + return 0; +} \ No newline at end of file diff --git a/competitive_programming/equalornotcf.py b/competitive_programming/equalornotcf.py new file mode 100644 index 0000000..9714263 --- /dev/null +++ b/competitive_programming/equalornotcf.py @@ -0,0 +1,8 @@ +t = int(input()); +for i in range (t): + s = input() + if s[0] == s[len(s) - 1]: + print("YES") + else: + print("NO") + diff --git a/competitive_programming/finaldayatc.cpp b/competitive_programming/finaldayatc.cpp new file mode 100644 index 0000000..00cc3bc --- /dev/null +++ b/competitive_programming/finaldayatc.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +int main() +{ + int n, k, i; + cin >> n >> k; + int x, y, z, totalpoints[n], sorttotalpoints[n]; + for ( i = 0; i < n; i++) + { + cin >> x >> y >> z; + totalpoints[i] = x+y+z; + sorttotalpoints[i] = x+y+z; + } + int asize = sizeof(sorttotalpoints) / sizeof(sorttotalpoints[0]); + sort(sorttotalpoints, sorttotalpoints + asize); + for ( i = 0; i < n; i++) + { + if(totalpoints[i] + 300 >= sorttotalpoints[n-k]) cout << "Yes" << endl; + else cout << "No" << endl; + } + return 0; +} diff --git a/competitive_programming/findarrcf.cpp b/competitive_programming/findarrcf.cpp new file mode 100644 index 0000000..11ab961 --- /dev/null +++ b/competitive_programming/findarrcf.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +int main() +{ + int t, n; + cin >> t; + while (t--) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cout << i + 2 << " "; + } + cout << endl; + + } + +} \ No newline at end of file diff --git a/competitive_programming/learningdp/editdistancelt.cpp b/competitive_programming/learningdp/editdistancelt.cpp new file mode 100644 index 0000000..1022553 --- /dev/null +++ b/competitive_programming/learningdp/editdistancelt.cpp @@ -0,0 +1,44 @@ +#include +#define EMPTY_VALUE -1; +using namespace std; +int mem[20][20]; +int ed(string &s, string &w, int i, int j) +{ + int ans, del, ins, change, tmp; + ans = del = ins = change = tmp = 0; + if (mem[i][j] != EMPTY_VALUE) + return mem[i][j]; + else if (i == s.size()) + ans += w.size() - s.size(); + else if (j == w.size()) + ans = ans; + else + { + if (s[i] == w[j]) + ans = ed(s, w, i + 1, j + 1); + else + { + del = 1 + ed(s, w, i + 1, j); + change = 1 + ed(s, w, i + 1, j + 1); + ins = 1 + ed(s, w, i, j + 1); + tmp = min(ins, change); + ans = min(del, tmp); + } + } + mem[i][j] = ans; + return ans; +} + +int main() +{ + string s = "horse"; + string w = "ros"; + for (int i = 0; i < 20; i++) + { + for (int j = 0; j < 20; j++) + { + mem[i][j] = EMPTY_VALUE; + } + } + cout << ed(s, w, 0, 0) << endl; +} \ No newline at end of file diff --git a/competitive_programming/learningdp/fibRecursive.cpp b/competitive_programming/learningdp/fibRecursive.cpp new file mode 100644 index 0000000..a92d914 --- /dev/null +++ b/competitive_programming/learningdp/fibRecursive.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; +int way (int n); +int main() +{ + int n; + cin >> n; + cout << way(n); +} + +int way(int n) +{ + if(n==0 || n==1) return 1; + return way(n-1) + way(n-2); +} diff --git a/competitive_programming/learningdp/fibiterativedp.cpp b/competitive_programming/learningdp/fibiterativedp.cpp new file mode 100644 index 0000000..9e47c9e --- /dev/null +++ b/competitive_programming/learningdp/fibiterativedp.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; +int main() +{ + int n, i; + cin >> n; + int way[n+1]; + way[0] = way[1] = 1; + for ( i = 2; i <= n; i++) + { + way[i] = way[i-1] + way[i-2]; + } + cout << way[n]; +} \ No newline at end of file diff --git a/competitive_programming/learningdp/lcs.cpp b/competitive_programming/learningdp/lcs.cpp new file mode 100644 index 0000000..42108df --- /dev/null +++ b/competitive_programming/learningdp/lcs.cpp @@ -0,0 +1,49 @@ +#include +#define EMPTY_VALUE -1; +using namespace std; +int memory[10][10]; +int lcs(int i, int j, string &s, string &w) +{ + if (i == s.size() || j == w.size())// here "abc" and i==3 so it is out of the string + return 0; + + if (memory[i][j] != -1) + { + return memory[i][j]; + } + + int ans = 0; + if (s[i] == w[j]) + { + ans = 1 + lcs(i + 1, j + 1, s, w); + } + else + { + int val1 = lcs(i + 1, j, s, w); + int val2 = lcs(i, j + 1, s, w); + + ans = max(val1, val2); + } + + memory[i][j] = ans; // two dimentional array why: we will have lcs for both strings + return memory[i][j]; + return s.size(); +} + +int main() +{ + string s = "hellom", w = "hmrll"; + for (int i = 0; i < 10; i++) + { + for(int j = 0; j<10; j++) + memory[i][j] = EMPTY_VALUE; + } + + cout << lcs(0, 0, s, w) < +using namespace std; + +int highestincresubseqofthatindex[7]; +int f(int x) +{ + int array[7] = {5, 0, 9, 2, 7, 3, 4}; + int max = 0, incresub = 0; // if you don't initialize incresub = 0 please see what happens. it happens because in recursion it repeats again -- and every time the incresub becomes some garbage values + if (highestincresubseqofthatindex[x] != 0) + return highestincresubseqofthatindex[x]; + else + { + highestincresubseqofthatindex[x] = 0; + for (int i = x + 1; i < 7; i++) + { + if (array[i] >= array[x]) + incresub = f(i); + if (incresub > max) + max = incresub; + } + + highestincresubseqofthatindex[x] = 1 + max; + return highestincresubseqofthatindex[x]; + } +} + +int main() +{ + + int i, lis = 0; + for (i = 0; i < 7; i++) + { + highestincresubseqofthatindex[i] = 0; + } + + for (i = 0; i < 7; i++) + { + f(i); + } + + for (i = 0; i < 7; i++) + { + if(highestincresubseqofthatindex[i] > lis) lis = highestincresubseqofthatindex[i]; + } + cout << lis << endl; +} \ No newline at end of file diff --git a/competitive_programming/learningdp/lisiterative.cpp b/competitive_programming/learningdp/lisiterative.cpp new file mode 100644 index 0000000..520038c --- /dev/null +++ b/competitive_programming/learningdp/lisiterative.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; +int main() +{ + int array[7] = {5,0,9,2,7,3,4}; + int highestincresubseqofthatindex[7]; + for (int i = 6 ; i >= 0; i++) + { + + } + +} \ No newline at end of file diff --git a/competitive_programming/learningdp/shafaetfibo.cpp b/competitive_programming/learningdp/shafaetfibo.cpp new file mode 100644 index 0000000..c25e635 --- /dev/null +++ b/competitive_programming/learningdp/shafaetfibo.cpp @@ -0,0 +1,22 @@ +#define MAX_N 20 +#define EMPTY_VALUE -1 + +int memo[MAX_N + 1]; + +int f(int n) { + if (n == 0) return 0; + if (n == 1) return 1; + + if (memo[n] != -1) { + return memo[n]; + } + + memo[n] = f(n - 1) + f(n - 2); + return memo[n]; +} + +void init() { + for (int i = 0; i <= MAX_N; i++) { + memo[i] = EMPTY_VALUE; + } +} \ No newline at end of file diff --git a/competitive_programming/learningdp/test.cpp b/competitive_programming/learningdp/test.cpp new file mode 100644 index 0000000..aa4544a --- /dev/null +++ b/competitive_programming/learningdp/test.cpp @@ -0,0 +1,26 @@ +#include +#define EMPTY_VALUE -1; +using namespace std; +int main() +{ + int memor[20][20]; + for (int i = 0; i < 20; i++) + { + for (int j = 0; j < 20; j++) + { + memor[i][j] == -1; + } + + } + for (int i = 0; i < 20; i++) + { + for (int j = 0; j < 20; j++) + { + cout< +using namespace std; +int main() { + int t, u, v; + cin >> t; + for (int i=0; i> u; + cin >> v; + cout << -u*u << " " << 1*v*v << "\n"; + } +} \ No newline at end of file diff --git a/competitive_programming/permutecf.cpp b/competitive_programming/permutecf.cpp new file mode 100644 index 0000000..0c758d7 --- /dev/null +++ b/competitive_programming/permutecf.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; +int main() { + int t, n, a, b, x, y; + cin >> t; + for (int i=0; i> n >> a >> b; + if ((a+b)>=n) + { + int leftarr[n/2], rightarr[n/2]; + x=y=0; + //a is min of leftarr + //b is max of rightarr + leftarr[x] = a; + x++; + rightarr[y] = b; + y++; + for (int i=1; i<=n; i++) + { + if (i!=a && i!=b) + { + if (ib) + { + leftarr[x] = i; + x++; + } + else + { + if (x < n/2) + { + leftarr[x] = i; + x++; + } + else + { + rightarr[y] = i; + y++; + } + } + } + } + for (i=0; i + +int main () { + int n, m; + std::cin >> n; + std::cin >> m; + int days=0; + for (int i=n; i>0; i--) + { + days++; + if (days%m == 0) + { + i++; //treating i as number of socks + } + } + std::cout << days; +} diff --git a/competitive_programming/takahashi.cpp b/competitive_programming/takahashi.cpp new file mode 100644 index 0000000..2519c50 --- /dev/null +++ b/competitive_programming/takahashi.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() { + int n, x, newlyknown, i; + cin >> n >> x; + newlyknown = x; + int kekare[n], knownornot[n]; + for ( i = 0; i < n; i++) + { + cin >> kekare[i]; + } + for ( i = 0; i < n; i++) + { + knownornot[i] = false; + } + + for ( i = 0; i < n; i++) + { + newlyknown = kekare[newlyknown - 1]; + if (knownornot[newlyknown - 1] == false) + knownornot[newlyknown - 1] = true; + else + break; + } + cout << i + 1 << endl; + + return 0; +} \ No newline at end of file diff --git a/competitive_programming/teamcf.cpp b/competitive_programming/teamcf.cpp new file mode 100644 index 0000000..6028622 --- /dev/null +++ b/competitive_programming/teamcf.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() { + int t; + cin >> t; + for(int i = 0 ; i < t ; i++){ + long long math, prog; + cin >> math >> prog; + long long maxteams = (int) (math + prog) / 4; + if(maxteams > math) + cout << math << endl; + else if(maxteams > prog) + cout << prog << endl; + else + cout << maxteams << endl; + } + return 0; +} \ No newline at end of file diff --git a/competitive_programming/teamcf.py b/competitive_programming/teamcf.py new file mode 100644 index 0000000..a0b9d6c --- /dev/null +++ b/competitive_programming/teamcf.py @@ -0,0 +1,11 @@ +t = int(input()) +for i in range(t): + math = int(input()) + prog = int(input()) + maxteams = int((math + prog)/4) + if maxteams > math: + print(math) + elif maxteams > prog: + print(prog) + else: + print(maxteams) \ No newline at end of file diff --git a/competitive_programming/template.cpp b/competitive_programming/template.cpp new file mode 100644 index 0000000..b8d5504 --- /dev/null +++ b/competitive_programming/template.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int main() { + int t, i; + cin >> t; + for(i = 0 ; i < t ; i++){ + cin >> ; + + } + return 0; +} + + +// to ignore after decimal point--> maxteams = (int) (math + prog) / 4; diff --git a/competitive_programming/vacationcfdp.cpp b/competitive_programming/vacationcfdp.cpp new file mode 100644 index 0000000..3906735 --- /dev/null +++ b/competitive_programming/vacationcfdp.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; +int days[100]; +int memory[100][3]; + +int vac(int i, int j, int prev) +{ + int ans = 0; + if (memory[i][j] != -1) + return memory[i][j]; + else if (days[i] == -1) + return 0; + else + { + + if (j == 0) + memory[i][j] = min(min(1 + vac(i + 1, 0), vac(i + 1, 1)), vac(i + 1, 2)); + if (j == 1) + memory[i][j] = min(1 + vac(i + 1, 0), vac(i + 1, 2)); + if (j == 2) + memory[i][j] = min(1 + vac(i + 1, 0), vac(i + 1, 1)); + + return memory[i][j]; + } +} + +int main() +{ + int n, i; + cin >> n; + for (i = 0; i < 1000; i++) + { + days[i] = -1; + memory[i] = -1; + } + + for (i = 0; i < n; i++) + { + cin >> days[i]; + } + + cout << min(min(vac(0, 0), vac(0, 1)) , vac(0, 2)) << endl; + + return 0; +} diff --git a/competitive_programming/vacationcfdp1.cpp b/competitive_programming/vacationcfdp1.cpp new file mode 100644 index 0000000..16ff364 --- /dev/null +++ b/competitive_programming/vacationcfdp1.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; +int days[100]; +int memory[100][3]; + +int vac(int i, int j) +{ + int ans = 0; + if (memory[i][j] != -1) + return memory[i][j]; + else + { + + if (j == 0) + memory[i][j] = min(min(1 + vac(i + 1, 0), vac(i + 1, 1)), vac(i + 1, 2)); + if (j == 1) + memory[i][j] = min(1 + vac(i + 1, 0), vac(i + 1, 2)); + if (j == 2) + memory[i][j] = min(1 + vac(i + 1, 0), vac(i + 1, 1)); + return memory[i][j]; + } +} + +int main() +{ + int n, i; + cin >> n; + for (i = 0; i < 100; i++) + { + days[i] = -1; + for (int j = 0; j < 3; j++) + { + memory[i][j] = -1; + } + } + + for (i = 0; i < n; i++) + { + cin >> days[i]; + } + + cout << min(min(vac(0, 0), vac(0, 1)), vac(0, 2)) << endl; + + + return 0; +} diff --git a/competitive_programming/vacationcfdp2.cpp b/competitive_programming/vacationcfdp2.cpp new file mode 100644 index 0000000..2fe4a04 --- /dev/null +++ b/competitive_programming/vacationcfdp2.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; +int days[100]; +int memory[100][3]; + +int vac(int index, int prev) +{ + int ans = 0, tmp = 0; + if (memory[index] != -1) + return memory[index]; + else if (days[index] == -1) + return 0; + else + { + if (prev != 0) + { + if (index == 1) + ans = min(1 + vac(index + 1, 0), vac(index + 1, 1)); + if (index == 2) + ans = min(1 + vac(index + 1, 0), vac(index + 1, 2)); + if (index == 3) + { + tmp = min(1 + vac(index + 1, 0), vac(index + 1, 1)); + ans = min(tmp, vac(index + 1, 2)); + } + } + else ans = 1 + vac(index + 1, 0); + memory[index] = ans; + return ans; + } + +} + +int main() +{ + int n, i; + cin >> n; + for (i = 0; i < 1000; i++) + { + days[i] = -1; + memory[i] = -1; + } + + for (i = 0; i < n; i++) + { + cin >> days[i]; + } + + cout << vac(0, 0); + for (i = 0; i < n; i++) + { + cout << days[i] << endl; + } + return 0; +}