From e0454f95eaff38279f975816ec2312e7e9fd37f9 Mon Sep 17 00:00:00 2001 From: Kunal Raut Date: Tue, 6 Oct 2020 11:18:37 +0530 Subject: [PATCH 1/2] Added Rod Cutting Problem DP Soln --- Rod_Cutting_Problem.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Rod_Cutting_Problem.cpp diff --git a/Rod_Cutting_Problem.cpp b/Rod_Cutting_Problem.cpp new file mode 100644 index 0000000..65d3694 --- /dev/null +++ b/Rod_Cutting_Problem.cpp @@ -0,0 +1,17 @@ +int main() +{ + //CODE FOR FINDING MAX VALUE OBTAINED BY CUTTING ROD INTO PIECES OF Ith SIZE GIVES PRICE OF A[i] AND FINDING FOR ROD OF LENGTH N WITH PRICE[0..N-1] GIVEN. + ll n; cin >> n; ll a[n]; + for (int i = 0; i < n; i++) cin >> a[i]; + int dp[n + 5] = {0}; + for (int i = 1; i <= n; i++) + { + int max_val = INT_MIN; + for (ll j = 0; j < i; j++) + { + max_val = max(max_val, a[j] + dp[i - j - 1]); //i-j is difference needed for j to complete till i + } + dp[i] = max_val; + } + cout << dp[n]; +} \ No newline at end of file From 51d437cfb56d26718eb6d0a7ef79bcb0c265461b Mon Sep 17 00:00:00 2001 From: Kunal Raut Date: Tue, 6 Oct 2020 12:06:38 +0530 Subject: [PATCH 2/2] Added Some Important Geometry Algorithms --- .DS_Store | Bin 0 -> 8196 bytes Convex_Hull.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ Sweepline.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 246 insertions(+) create mode 100644 .DS_Store create mode 100644 Convex_Hull.cpp create mode 100644 Sweepline.cpp diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6184370eee76a1137dd74a6877dd4e0ca81809e9 GIT binary patch literal 8196 zcmeI1&2AGh5XZ+*iByQT98g-|ut!dn5JxVdO(ZIT_{alJ+u4eU+dDgaEV3;k4YJCu+Y~iP`!!e6jiRNA zbl^pHWhf_da**Z2%IMe-8Hfx-1|kEIfyluBzyR-TO^pTbecv0)$UtOZB^l8BAt9@5 z6>{#ST^$q_0wC9DwnUvcz%WT6TZNo^X;5_f=)o0GSA-bO!|6|SII>m9xtAU;&cnqO zGrJ-}aW*@+#D`7PSy0wpBVb_*zWXqtdwhg{%kb>%68?_70QVP2WweC63sSl8 zCm~_4U7m^h6@=lEhD|)Z)KETE)%AhR5_&^kGUL>^T(Z-Rwex_rZNC2bz2;BvN1l`e zU1wdN3H^F+XnmKb!}4AcWiiyZA#c!-+SyrhZj8qeyDo2~BhRFbKb^YSbZ3@kYl6t_ zHrr>`89!xljP0-CBQH+>f?LulkKz1H{7&&xPvI+eWCtDUd2W2@8Sp!+c1?}h@Kioz zA1Ir8JC69lDTo~bzgH=8pEMpj#5l3%D_$U_qN9WoyFh(SCpOTc4S~R z7;tOtZhH@3i`cs2wZ_^B`5jr4j+=XF5DE*&krs|4FaBXjJz=O~T7{f@Nek(pe+amU OYvTLg79bWY$-r;%fY*rt literal 0 HcmV?d00001 diff --git a/Convex_Hull.cpp b/Convex_Hull.cpp new file mode 100644 index 0000000..25c57c2 --- /dev/null +++ b/Convex_Hull.cpp @@ -0,0 +1,130 @@ +struct point //Replace double with int if not required +{ + double x, y; + + point () {} + + point(int x, int y) : x(x), y(y) {} + + void operator =(const point &p) + { + x = p.x, y = p.y; + } + + bool operator <(const point&p) + { + if (x == p.x) + return y < p.y; + return x < p.x; + } + + point operator +(const point&p) const + { + point pt(x + p.x, y + p.y); + return pt; + } + + point operator -(const point&p) const + { + point pt(x - p.x, y - p.y); + return pt; + } + + double crossProduct(const point &p) const + { + return x * p.y - y * p.x; + } + + int dotProduct(const point &p) const + { + return x * p.x + y * p.y; + } + + double dist() + { + return x * x + y * y; + } +}; + + +bool comp(point &p1, point &p2) +{ + if (p1.x != p2.x) + return p1.x < p2.x; + return p1.y < p2.y; +} + +bool cw(point &a, point &b, point &c) +{ + int area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); + return area < 0; +} + +bool ccw(point &a, point &b, point &c) +{ + int area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); + return area > 0; +} +// 1 => Strictly inside; -1 => Border; 0 => Outside +int point_in_poly(const vector & poly, point p) { + int many = 0; + for (int i = 0; i < (int)poly.size(); i++) { + Point a = poly[i], b = poly[i + 1 < (int) poly.size() ? i + 1 : 0]; + if (a.x > b.x) swap(a, b); + if (a.x <= p.x && p.x <= b.x) { + if (abs(a.x - b.x) == 0) { + if (min(a.y, b.y) <= p.y && p.y <= max(a.y, b.y)) return -1; + } else { + double y = a.y + 1. * (b.y - a.y) / (b.x - a.x) * (p.x - a.x); + if (abs(y - p.y) <= E) return -1; + if (y >= p.y && p.x < b.x) many++; + } + } + } + return many % 2; +} +vector convex_hull(vector &v) +{ + if (v.size() == 1) + return v; + + sort(v.begin(), v.end(), comp); + + point p1 = v[0], p2 = v.back(); + + vector up, down; + up.push_back(p1); + down.push_back(p1); + + for (int i = 1; i < v.size(); i++) + { + if (i == v.size() - 1 || cw(p1, v[i], p2)) + { + while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], v[i])) + up.pop_back(); + up.push_back(v[i]); + } + if (i == v.size() - 1 || ccw(p1, v[i], p2)) + { + while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], v[i])) + down.pop_back(); + down.push_back(v[i]); + } + } + + for (int i = down.size() - 2; i > 0; i--) + up.push_back(down[i]); + + return up; +} + +//Problem 0: https://www.codechef.com/ACM16CHN/problems/CHN16B +//Solution 0: Print the sum of cost of all points in Convex Hull https://ideone.com/lEt7K1 + +//Problem 1 (Polygon Congruence): http://codeforces.com/contest/1017/problem/E +//Solution 1: http://codeforces.com/contest/1017/submission/41401690 + +//Problem 2 and Solution: http://codeforces.com/gym/101606/submission/41541222 + +//Problem 3:https://www.codechef.com/JUNE20A/problems/CONTAIN +//Solution 3: https://www.codechef.com/viewsolution/34657860 \ No newline at end of file diff --git a/Sweepline.cpp b/Sweepline.cpp new file mode 100644 index 0000000..4a59e04 --- /dev/null +++ b/Sweepline.cpp @@ -0,0 +1,116 @@ +/* + PRINTS AREA OF NESTED REACTANGLE UNION USING SWEEPLINE +*/ + +/* + KUNAL RAUT (ZUKONIT14) :- "DO WHAT YOU LIKE!" + -PICT,PUNE! :) + */ +#include +using namespace std; +#define ll long long +#define int long long +#define ld long double +#define pb push_back +#define mp make_pair +#define fi first +#define se second +#define mod 1000000007 +#define mod9 1000000009 +#define pi 3.1415926535 +#define MAXN 1000005 +#define N 1000001 +#define MAX2N 1*1000 + 10 +#define all(v) v.begin(), v.end() +#define allr(v) v.rbegin(), v.rend() +#define ms(s, n) memset(s, n, sizeof(s)) +#define prec(n) fixed<>t;for(ll testi=1;testi<=t;testi++) +#define djokovic freopen("input00.txt", "r", stdin);freopen("output00.txt", "w", stdout); +ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);} +ll lcm(ll a, ll b) { return (a / gcd(a, b) * b);} +ll expo(ll x, ll y) { + ll res = 1; x = x % mod; while (y > 0) { + if (y & 1)res = (1ll * res * x) % mod; + y = y >> 1; x = (1ll * x * x) % mod; + } return res; +} +ll ncr(ll n, ll r) { ll res = 1; if (r > n - r)r = n - r; for (ll i = 0; i < r; i++) { res *= n - i; res /= i + 1; } return res;} +ll max(ll a, ll b) {return (a > b) ? a : b;} +bool prime(ll n) {ll i; for (i = 2; i <= sqrt(n); i++) {if (n % i == 0)return false;} return true;} +bool sortbysec(const pair &a, const pair &b) +{ if (a.second == b.second) + return a.first < b.first; + return (a.second < b.second); +} + +//https://www.youtube.com/watch?v=WTJSt4wP2ME + +ll rr[] = {0, 1, 1, 1, 0, -1, -1, -1}; +ll cc[] = {1, 1, 0, -1, -1, -1, 0, 1}; +struct event +{ + ll x, t, y1, y2; +}; +bool comp(const event &a, const event &b) +{ + if (a.x == b.x) + return (a.t < b.t); + return (a.x < b.x); +} +ll n, k, ans, p[MAXN], a[MAXN], vis[MAXN]; +vectorve; +multisetlow, up; +void solve() +{ + cin >> n; + for (ll i = 1; i <= n; i++) + { + ll x1, x2, y1, y2; cin >> x1 >> x2 >> y1 >> y2; + ve.pb({x1, 1, y1, y2}); + ve.pb({x2, -1, y1, y2}); + } + sort(all(ve), comp); + ll laste = ve[0].x; + for (auto e : ve) + { + //cout << e.x << " " << e.t << "\n"; + if (e.t == 1) //ADDING EVENT PUTTING Y1 INTO LOW SET AND Y2 IN UP SET AND TAKING THE MAX IN UP SET AND MIN IN LOW SET AND TAKING THEIR DIFFERENCE AND MUKLTIPLE NY X DIFF + { + if (up.size()) + { + auto it = up.end(); it--; + auto it1 = low.begin(); + ans += ((*it - *it1) * (e.x - laste)); + } + up.insert(max(e.y1, e.y2)); + low.insert(min(e.y1, e.y2)); + } + else //REMOVING EVENT ALSO ERASING Y1 AND Y2 AND TAKING ANSWER AND THEN REMOVING THE Y1 AND Y2 + { + auto it = up.end(); it--; + auto it1 = low.begin(); + ans += ((*it - *it1) * (e.x - laste)); + up.erase(up.find(max(e.y1, e.y2))); + low.erase(low.find(min(e.y1, e.y2))); + } + laste = e.x; + //cout << ans << " " << e.x << " " << e.t << " " << "\n"; + } + cout << ans; +} +signed main() +{ + bolt; +#ifndef ONLINE_JUDGE + djokovic; +#endif + solve(); +} + +//SOMETHING LIKE THIS : https://codeforces.com/blog/entry/20377 +// FOR TGHE CODE ALIGNED THE BASE ON OX JUST TAKING ONE Y AND TAKIBNG THAT SET'S TOP WITH X DIFF \ No newline at end of file