From ff432154f4a8021ff146640e4ca4990adc847393 Mon Sep 17 00:00:00 2001 From: Paras Jain <55691273+parasjain2792@users.noreply.github.com> Date: Fri, 15 Oct 2021 22:30:23 +0530 Subject: [PATCH 1/5] Create MehtaAndBankRObbery.cpp --- .../MehtaAndBankRObbery.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Dynamic Programming - 2/MehtaAndBankRObbery.cpp diff --git a/Dynamic Programming - 2/MehtaAndBankRObbery.cpp b/Dynamic Programming - 2/MehtaAndBankRObbery.cpp new file mode 100644 index 0000000..184c1c0 --- /dev/null +++ b/Dynamic Programming - 2/MehtaAndBankRObbery.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; +#define ll long long +int main(){ + int t; + cin>>t; + while(t--){ + ll n,wi; + cin>>n>>wi; + pair *arr=new pair[n]; + for(int i=0;i>arr[i].first; + } + for(int i=0;i>arr[i].second; + } + sort(arr,arr+n); + ll prime[11]={0,2,3,5,7,11,13,17,19,23,29}; + ll ***dp=new ll**[2]; + for(int i=0;i<2;i++){ + dp[i]=new ll *[n+1]; + for(int j=0;j<=n;j++){ + dp[i][j]=new ll[wi+1]; + for(int k=0;k<=wi;k++){ + dp[i][j][k]=0; + } + } + } + //base case + for(int i=1;i<=n;i++){ + for(int j=1;j<=wi;j++){ + dp[0][i][j]=dp[0][i-1][j]; + if(j>=arr[i-1].second){ + dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j-arr[i-1].second]+arr[i-1].first); + } + } + } + //follow-up + int curr=0; + for(int p=1;p<=10;p++){ + curr=p%2; + for(int i=1;i<=n;i++){ + for(int j=1;j<=wi;j++){ + dp[curr][i][j]=dp[curr][i-1][j]; + if(j>=arr[i-1].second) + dp[curr][i][j]=max(dp[curr][i][j],max(dp[curr][i-1][j-arr[i-1].second]+arr[i-1].first,dp[curr^1][i-1][j-arr[i-1].second]+arr[i-1].first*prime[p])); + } + } + } + cout< Date: Thu, 21 Oct 2021 02:01:34 +0530 Subject: [PATCH 2/5] Update MehtaAndBankRObbery.cpp --- .../MehtaAndBankRObbery.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Dynamic Programming - 2/MehtaAndBankRObbery.cpp b/Dynamic Programming - 2/MehtaAndBankRObbery.cpp index 184c1c0..39c906c 100644 --- a/Dynamic Programming - 2/MehtaAndBankRObbery.cpp +++ b/Dynamic Programming - 2/MehtaAndBankRObbery.cpp @@ -1,3 +1,30 @@ +/* +One fine day, when everything was going good, Mehta was fired from his job and had to leave all the work. So, he decided to become a member of gangster squad and start his new career of robbing. Being a novice, mehta was asked to perform a robbery task in which he was given a bag having a capacity W units. +So, when he reached the house to be robbed, there lay N items each having particular weight and particular profit associated with it. But, theres a twist associated, +He has first 10 primes with him, which he can use atmost once, if he picks any item x, then he can multiply his profit[x] with any of the first 10 primes and then put that item into his bag. +Each prime can only be used with one particular item and one item can only have atmost one prime multiplied with its profit. Its not necessary to pick all the items. +If he doesnt want to use a prime with any particular item, he can simply add the profit as it is, more specifically, 1*profit[x] for xth item will get added to its total profit, and that he can do with as many items as he wants. +He cannot fill his bag more than weight W units. Each item should be picked with its whole weight, i.e it cannot be broken into several other items of lesser weight. +So, now to impress his squad, he wishes to maximize the total profit he can achieve by robbing this wealthy house. +Input Format : +The first line of input will contain T(number of test cases), each test will follow as. +First Line will contain two integers N and W (number of items and maximum weight respectively). +Second-line will contain N space-separated integers denoting the profit associated with the Ith item. +The third line will contain N space-separated integers denoting the weight of the Ith item. +Constraints: +1 <= T <= 20 +1 <= N, W <= 500 +1 <= profit[i] <= 10^4 +1 <= weight[i] <= 10^4 +Output the maximum profit obtainable for each test case in a new line. +Sample Input: +1 +7 37 +33 5 14 14 16 25 15 +5 19 30 4 15 31 25 +Sample output: +1591 + */ #include using namespace std; #define ll long long From 017aa364e252f94ca834d79d0a82a9092da2f64d Mon Sep 17 00:00:00 2001 From: Paras Jain <55691273+parasjain2792@users.noreply.github.com> Date: Thu, 21 Oct 2021 02:04:09 +0530 Subject: [PATCH 3/5] Update MehtaAndBankRObbery.cpp --- Dynamic Programming - 2/MehtaAndBankRObbery.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Dynamic Programming - 2/MehtaAndBankRObbery.cpp b/Dynamic Programming - 2/MehtaAndBankRObbery.cpp index 39c906c..0993c64 100644 --- a/Dynamic Programming - 2/MehtaAndBankRObbery.cpp +++ b/Dynamic Programming - 2/MehtaAndBankRObbery.cpp @@ -76,5 +76,6 @@ int main(){ } cout< Date: Sat, 30 Oct 2021 16:34:22 +0530 Subject: [PATCH 4/5] effecient and easy to understand code --- DP and BitMasking/StringMaker.cpp | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 DP and BitMasking/StringMaker.cpp diff --git a/DP and BitMasking/StringMaker.cpp b/DP and BitMasking/StringMaker.cpp new file mode 100644 index 0000000..9632e96 --- /dev/null +++ b/DP and BitMasking/StringMaker.cpp @@ -0,0 +1,66 @@ +/*According to Ancient Ninjas , string making is an art form . There are various methods of string making , one of them uses previously generated strings to make the new one . Suppose you have two strings A and B , to generate a new string C , you can pick a subsequence of characters from A and a subsequence of characters from B and then merge these two subsequences to form the new string. +Though while merging the two subsequences you can not change the relative order of individual subsequences. What this means is - suppose there two characters Ai and Aj in the subsequence chosen from A , where i < j , then after merging if i acquires position k and j acquires p then k

+using namespace std; +#define ll long long +int mod=1e9+7; +ll solver(string &a,string &b,string &c,ll x,ll y,ll z,ll dp[][51][51]){ + if(z==0){ + return 1; + } + if(x<=0 && y<=0){ + return 0; + } + if(dp[x][y][z]!=-1){ + return dp[x][y][z]; + } + int ways=0; + for(int i=x-1;i>=0;i--){ + if(a[i]==c[z-1]) + ways=(ways+solver(a,b,c,i,y,z-1,dp))%mod; + } + for(int j=y-1;j>=0;j--){ + if(b[j]==c[z-1]) + ways=(ways+solver(a,b,c,x,j,z-1,dp))%mod; + } + dp[x][y][z]=ways; + return dp[x][y][z]; +} +ll solve(string a,string b,string c){ + ll lena=a.length(); + ll lenb=b.length(); + ll lenc=c.length(); + ll dp[51][51][51]; + memset(dp,-1,sizeof(dp)); + return solver(a,b,c,lena,lenb,lenc,dp); +} +int main(){ + int t; + cin>>t; + while(t--){ + string a,b,c; + cin>>a>>b>>c; + cout< Date: Sun, 31 Oct 2021 00:35:59 +0530 Subject: [PATCH 5/5] another solution --- Adhoc Problems/winning-Strategy.cpp | 139 ++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Adhoc Problems/winning-Strategy.cpp diff --git a/Adhoc Problems/winning-Strategy.cpp b/Adhoc Problems/winning-Strategy.cpp new file mode 100644 index 0000000..435e789 --- /dev/null +++ b/Adhoc Problems/winning-Strategy.cpp @@ -0,0 +1,139 @@ +/* +The college team, along with their coach, is going to the sports fest to play a football match. There are n players in the team, numbered from 1 to n. +Someone gives a paper to the coach. The paper elaborates on the positions and strategies of the opponent team. Based on it, the coach creates a winning strategy. In that strategy, he decides and gives a particular position to every player. +After this, the coach starts swapping two players at a time to make them stand according to new positions decided on paper. +He swaps players by applying following rules: +1. Any player can swap with the player standing next to him. +2. One player can swap with at most two other players. +Given that initially all the players are standing linearly, numbered from 1 to n, you have to tell whether it is possible for the coach to create new positions by swapping within the constraints defined in the task. +Input Format +The first line of input will contain an integer, that denotes the value of the number of test cases. Let us denote the number of test cases by the symbol T. +Each of the following T test cases consists of two lines. The first line of each test case contains an integer n, that denotes the number of players in the team. The following line contains n space separated integers, denoting the specific position of players in winning strategy. +Output Format +For each test case, if it is possible to create winning strategy positions, then print "YES" (without quotes) and in the next line, print the minimum numbers of swaps required to form the winning strategy order, otherwise print "NO"(without quotes) in a new line. +Constraints +1 <= T <= 50 +1 =< N <= 10^5 +1 <= A[i] <= n +Time Limit: 1 second +Sample Input 1: +1 +5 +2 1 5 3 4 +Sample Output 1: +YES +3 +Explanation +In this case, we can achieve winning strategy positions in 3 swaps. Initial state of positions: 1 2 3 4 5 +Three moves required to form winning strategy positions: +1 2 3 4 5 -> 1 2 3 5 4 -> 1 2 5 3 4 -> 2 1 5 3 4 +Sample Input 2: +1 +5 +2 5 1 3 4 +Sample Output 2: +NO +Explanation: +In the second case, there is no way to form the specific winning strategy positions by swapping within the constraints mentioned in the task. +*/ +#include +using namespace std; +#define ll long long +int main(){ + ll t; + cin>>t; + while(t--){ + ll n; + cin>>n; + ll arr[n]={0}; + ll flag=0; + for(int i=0;i>arr[i]; + if(abs(arr[i]-(i+1))>2) + flag=1; + } + if(flag==1){ + cout<<"NO"<arr[i+1]){ + swap(arr[i],arr[i+1]); + count++; + } + } + + + for(int i=0;iarr[i+1]) + { + gh=1; + break; + } + } + if(gh==1){ + cout<<"NO"< +using namespace std; +#define ll long long +int main() { + + // Write your code here + int n; + cin>>n; + int a[n]; + for(int i=0;i>a[i]; + + } + int count =0; + for(int i=0;i<(n-1);i++){ + + if(a[i+1]