diff --git a/C++/Dynamic Programming/interval_queries.cpp b/C++/Dynamic Programming/interval_queries.cpp new file mode 100644 index 0000000..3f0d643 --- /dev/null +++ b/C++/Dynamic Programming/interval_queries.cpp @@ -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< +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< ## Graph Algorithms @@ -66,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 diff --git a/README.md b/README.md index 5b70072..e9a97a7 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) @@ -83,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)