From d690a3e4484579e7d598f17f58dec79e2335f6b0 Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Mon, 19 Oct 2020 09:59:37 +0530 Subject: [PATCH 01/11] Create interval_queries This is a detailed solution to provide intuition on solving CP problems based on answering queries about time interval intersection. Have a look into at the question: http://codeforces.com/gym/294377/problem/E --- C++/Dynamic Programming/interval_queries | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 C++/Dynamic Programming/interval_queries diff --git a/C++/Dynamic Programming/interval_queries b/C++/Dynamic Programming/interval_queries new file mode 100644 index 0000000..3f0d643 --- /dev/null +++ b/C++/Dynamic Programming/interval_queries @@ -0,0 +1,49 @@ +#include +using namespace std; + +int main() { + // your code goes here + // this program will give an intuition behind the solution for CP questions based on interval quries. Refer to the question + //http://codeforces.com/gym/294377/problem/E + + //this following two lines are used for fast input-output + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin>>t; + while(t--) //running the program for 't' test cases + { + int n; + cin>>n; //number of students in th college + + //declare an array of max size (based on the input constraint) to indicate the timeline of all the students + int N=2*100000; + int a[N]={0}; + + for(int i=0;i>x>>y; + // while taking the entering and leaving time of each student, increase the (x-1)th index by one and decrease the (y-1)th index by 1. + a[x-1]+=1; + a[y-1]-=1;// Here the student is outside the college during the yth sec, hence we subtract 1 from (y-1)th index. Else we would subtract the same from yth index + } + for(int i=1;i<2*100000;i++) + { + // Summing the value in the current index by its previous index's value will give the total number of students at a perticular instance of time on the timeline a[N] + a[i]+=a[i-1]; + } + int q; + cin>>q; + while(q--)// running a loop to ans all the queries + { + int pp; + cin>>pp;// The required time to output number of students present at that particular instance + + // We can find out the number of students present at that perticular time by accessing the value stored in the array 'a' at (pp-1) index + cout< Date: Mon, 19 Oct 2020 10:03:40 +0530 Subject: [PATCH 02/11] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5b70072..a80c01f 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ You can also create an issue or [contact us](https://github.com/aniketsharma0041 - [Coin change](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/dp_coin_change.cpp) - [Longest Common Subsequence](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/lcs.cpp) +- [Interval_queries](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/interval_queries) From 7a38b044828d506c588e70ef1bf087656bfc5996 Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Mon, 19 Oct 2020 10:04:48 +0530 Subject: [PATCH 03/11] Update README.md --- C++/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/C++/README.md b/C++/README.md index 52cdcbe..1aaf0e9 100644 --- a/C++/README.md +++ b/C++/README.md @@ -22,6 +22,8 @@ - [Longest Common Subsequence](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/lcs.cpp) +- [Interval_queries](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/interval_queries) + ## Graph Algorithms From 9e655364a33df5d883e578ba6531c7012d5f5f58 Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Mon, 19 Oct 2020 10:05:55 +0530 Subject: [PATCH 04/11] Create Particular_sum This is a detailed solution to provide intuition on solving CP problems based on finding the reduced max size of a given array whose elements are divisible by given number. (Elements of the array can be replaced by sum of elements present in the array). Have a look into at the question: https://codeforces.com/problemset/problem/1176/B --- C++/Math/Particular_sum | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 C++/Math/Particular_sum diff --git a/C++/Math/Particular_sum b/C++/Math/Particular_sum new file mode 100644 index 0000000..3753bd8 --- /dev/null +++ b/C++/Math/Particular_sum @@ -0,0 +1,32 @@ +//This is a detailed solution to provide intuition on solving CP problems based on finding the reduced max size of a given array whose elements are divisible by given number. (Elements of the array can be replaced by sum of elements present in the array). +//Have a look into at the question: https://codeforces.com/problemset/problem/1176/B + +#include +using namespace std; + +int main() { + int t; + cin>>t; // Input to run the program for 't' test cases + while(t--){ + int n,f=0,r1=0,r2=0; + cin>>n; // Input to know the total number of elements in the array + vectora; // array initialization (a vector can also be used as an array) + for(int i=0;i>x; + if(x%3==0) + f++; // counting the number of elements which are divisible by 3 + if(x%3==1) + r1++; // counting the number of elements which leave reminder 1 when divided by 3 + if(x%3==2) + r2++; // counting the number of elements which leave reminder 2 when divided by 3 + } + int ans=f; + int m=min(r1,r2); // We can add-up the elements whose reminder is 1 with those whose reminder is 2 such that the newly formed number is divisible by 3 + ans+=m; + m=(r1+r2-2*m)/3; // Consider r1 as a set and r2 as a set. The remaining elements of a particular set, the one which has max elements and are not pared with the other remainder set but can still yield a sum which is divisible by 3. + ans+=m; + cout< Date: Mon, 19 Oct 2020 10:07:06 +0530 Subject: [PATCH 05/11] Update README.md --- C++/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/C++/README.md b/C++/README.md index 1aaf0e9..860fc16 100644 --- a/C++/README.md +++ b/C++/README.md @@ -68,6 +68,8 @@ - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp) +-[Particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum) + ## Searching From ad492e83d301e538283c0dd72f3da37fab88e609 Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Mon, 19 Oct 2020 10:07:56 +0530 Subject: [PATCH 06/11] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a80c01f..4f02572 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ You can also create an issue or [contact us](https://github.com/aniketsharma0041 - [Matrix exponentiation](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/matrix_exponentiation.cpp) - [Pascal triangle](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pascal_triangle.cpp) - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp) +-[Particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum) From b2bde64498f6ae29807dba616a6f4f1d8fc8590b Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Tue, 20 Oct 2020 11:23:37 +0530 Subject: [PATCH 07/11] Rename interval_queries to interval_queries.cpp This is a detailed solution to provide intuition on solving CP problems based on answering queries about time interval intersection. Have a look into at the question: http://codeforces.com/gym/294377/problem/E --- .../{interval_queries => interval_queries.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C++/Dynamic Programming/{interval_queries => interval_queries.cpp} (100%) diff --git a/C++/Dynamic Programming/interval_queries b/C++/Dynamic Programming/interval_queries.cpp similarity index 100% rename from C++/Dynamic Programming/interval_queries rename to C++/Dynamic Programming/interval_queries.cpp From e8aa2015d4083f8377ab833956a908b37364e6c5 Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Tue, 20 Oct 2020 11:24:18 +0530 Subject: [PATCH 08/11] Rename Particular_sum to Particular_sum.cpp This is a detailed solution to provide intuition on solving CP problems based on finding the reduced max size of a given array whose elements are divisible by given number. (Elements of the array can be replaced by sum of elements present in the array). Have a look into at the question: https://codeforces.com/problemset/problem/1176/B --- C++/Math/{Particular_sum => Particular_sum.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C++/Math/{Particular_sum => Particular_sum.cpp} (100%) diff --git a/C++/Math/Particular_sum b/C++/Math/Particular_sum.cpp similarity index 100% rename from C++/Math/Particular_sum rename to C++/Math/Particular_sum.cpp From d5c1597d5eded9428e948d0aa83e22dcafeb4afe Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Tue, 20 Oct 2020 11:31:22 +0530 Subject: [PATCH 09/11] Rename Particular_sum.cpp to particular_sum.cpp This is a detailed solution to provide intuition on solving CP problems based on finding the reduced max size of a given array whose elements are divisible by given number. (Elements of the array can be replaced by sum of elements present in the array). Have a look into at the question: https://codeforces.com/problemset/problem/1176/B --- C++/Math/{Particular_sum.cpp => particular_sum.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C++/Math/{Particular_sum.cpp => particular_sum.cpp} (100%) diff --git a/C++/Math/Particular_sum.cpp b/C++/Math/particular_sum.cpp similarity index 100% rename from C++/Math/Particular_sum.cpp rename to C++/Math/particular_sum.cpp From 0fbc88b9cd51e4cb498e218f15b0113363bf6742 Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Tue, 20 Oct 2020 11:33:26 +0530 Subject: [PATCH 10/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f02572..e9a97a7 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ You can also create an issue or [contact us](https://github.com/aniketsharma0041 - [Matrix exponentiation](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/matrix_exponentiation.cpp) - [Pascal triangle](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pascal_triangle.cpp) - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp) --[Particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum) +-[particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum) From c5602448ea30656f6b7f9d52276d4beed0a29e0b Mon Sep 17 00:00:00 2001 From: Kesharaju Varun Date: Tue, 20 Oct 2020 11:33:47 +0530 Subject: [PATCH 11/11] Update README.md --- C++/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C++/README.md b/C++/README.md index 860fc16..192958c 100644 --- a/C++/README.md +++ b/C++/README.md @@ -68,7 +68,7 @@ - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp) --[Particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum) +-[particular_sum](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/Particular_sum)