Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Assignment 4 - Ankit Jain #160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CP/Assignment4-Bit_Manipulation_Bit_Masks/Ankit Jain/484A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll monk(ll l, ll r)
{
if ((l|l+1)> r)
return l;
else
return monk(l|l+1,r);
}
main()
{
long long l,r;
int n;
cin>>n;
while(n--)
{
cin>>l>>r;
cout<<monk(l,r)<<endl;
}
}
28 changes: 28 additions & 0 deletions CP/Assignment4-Bit_Manipulation_Bit_Masks/Ankit Jain/535B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{
string s;
cin>>s;
int m=0;
queue<int>q ;
for(int i=0;i<s.size();i++)
{
if(s[i] == '4')
q.push(1);
else
q.push(0);
}
for( int i= s.size(); i>0 ; i--)
{
m+=(1 << i) ;
}
int n=0;
for(int i=(s.size()-1) ; i>=0 ; i--)
{
n+= (q.front()*(1<<i));
q.pop();
}
cout<<m-n;
}
26 changes: 26 additions & 0 deletions CP/Assignment4-Bit_Manipulation_Bit_Masks/Ankit Jain/550B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;
main()
{
int n,l,r,x,i=0,flag=0,sum=0,a1,a2;
long a[15];
cin>>n>>l>>r>>x;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int mask=0; mask<(1ll<<n); mask++)
{
sum=0;
for(int j=0; j<n; j++)
if(mask & (1ll<<j))
{
if (sum==0)
a1=a[j];
sum+=a[j];
a2=a[j];
}
if ((sum>=l && sum<=r) && ((a2-a1)>=x))
flag++;
}
cout<<flag<<endl;
}
29 changes: 29 additions & 0 deletions CP/Assignment4-Bit_Manipulation_Bit_Masks/Ankit Jain/xxor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>
using namespace std;
main()
{
long long x=0, a[100001];
int n,q;
cin>>n>>q;
int prefix[32][n+1],l,r,p,i,j;
for(i=1;i<=n;i++)
cin>>a[i];
for(i=0;i<31;i++)
{
prefix[i][0]=0;
for(j=1;j<=n;j++)
prefix[i][j]=prefix[i][j-1]+bool(a[j]&(1<<i));
}
while(q--)
{
cin>>l>>r;
x=0;
for(i=0;i<31;i++)
{
p=prefix[i][r]-prefix[i][l-1];
if (p<r-l+1-p)
x|=(1<<i);
}
cout<<x<<endl;
}
}
13 changes: 13 additions & 0 deletions CP/Assignment7-DynamicProgramming/Ankit Jain/518D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<bits/stdc++.h>
using namespace std;
double dp[2001][2001],p;
main()
{
memset(dp,0,sizeof(dp));
int n,i,j,t;
cin>>n>>p>>t;
for(int i=1;i<=n;i++)
for(int j=1;j<=t;j++)
dp[i][j]=p*dp[i-1][j-1]+dp[i][j-1]*(1-p)+p;
printf("%0.6f",dp[n][t]);
}
33 changes: 33 additions & 0 deletions CP/Assignment7-DynamicProgramming/Ankit Jain/580D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
using namespace std;
#define maxx 20
typedef long long ll;
ll dp[1<<maxx][20],a[20],c,n,k,x,i,y,m,benefit[20][20];
ll solve(ll mask, ll prev)
{
if (__builtin_popcount(mask)==m)
return 0;
if (dp[mask][prev]!=-1)
return dp[mask][prev];
ll ans=-1;
for(int i=0;i<n;i++)
if ((mask&(1<<i))==0)
ans = max(ans,a[i] + solve(mask | (1<<i), i) + benefit[prev][i]);
return dp[mask][prev]=ans;
}
main()
{
memset(dp,-1,sizeof(dp));
memset(benefit,0,sizeof(benefit));
cin>>n>>m>>k;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<k;i++)
{
cin>>x>>y>>c;
x--;
y--;
benefit[x][y]=c;
}
cout<<solve(0,19)<<endl;
}
26 changes: 26 additions & 0 deletions CP/Assignment7-DynamicProgramming/Ankit Jain/753A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;
main()
{
int n,sum[50]={0},i,j,d;
cin>>n;
for(i=1;i<50;i++)
sum[i]=i*(i+1)/2;
for(i=1;i<50;i++)
if (n<=sum[i])
break;
d=sum[i]-n;
if (d==0)
{
cout<<i<<endl;
for(j=1;j<=i;j++)
cout<<j<<" ";
}
else
{
cout<<i-1<<endl;
for(j=1;j<=i;j++)
if(j!=d)
cout<<j<<" ";
}
}
23 changes: 23 additions & 0 deletions CP/Assignment7-DynamicProgramming/Ankit Jain/Vaishu_n_tower.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
using namespace std;
main()
{
int i, s[100001]={0},x,n,t,res;
cin>>t;
while(t--)
{
cin>>n;
for(i=1;i<=n;i++)
{
cin>>x;
if (x==-1)
s[i]=s[i-1]+1;
else
s[i]=s[i-1];
}
res=1-s[1]+s[n]-s[1];
for(i=2;i<=n;i++)
res=min(res,i-s[i]+s[n]-s[i]);
cout<<res<<endl;
}
}
24 changes: 24 additions & 0 deletions CP/Assignment7-DynamicProgramming/Ankit Jain/project_euler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;
int t,n,dp[100][100],a[100][100],i,j;
int fun(int i, int j)
{
if (dp[i][j]!=-1)
return dp[i][j];
if (i==(n-1))
return dp[i][j]=a[i][j];
return dp[i][j]= max(fun(i+1,j), fun(i+1,j+1)) + a[i][j];
}
main()
{
cin>>t;
while(t--)
{
cin>>n;
memset(dp,-1,sizeof(dp));
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
cin>>a[i][j];
cout<<fun(0,0)<<endl;
}
}
127 changes: 127 additions & 0 deletions CP/Assignment8-Graphs/Ankit Jain/bishu_and_his_girlfriend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include<bits/stdc++.h>


using namespace std;


bool vis[1001];

vector<int>adj[1001];

int g[1001]={0};

int bfs(int s)

{

vis[s]=true;


queue <int> q;


q.push(s);


while(!q.empty())


{


int v=q.front();


q.pop();


for(int i=0;i<adj[v].size();i++)


{


if (!vis[adj[v][i]])


{


q.push(adj[v][i]);


vis[adj[v][i]]=true;

if (g[adj[v][i]])

return adj[v][i];


}


}


}


}


int main()


{

int n,q,x,y,i;


cin>>n;


for(i=1;i<=n;i++)


{


vis[i]=false;


}


for(i=1;i<n;i++)


{


cin>>x>>y;


adj[x].push_back(y);


adj[y].push_back(x);


}


cin>>q;

for(i=1;i<=q;i++)

{

cin>>x;

g[x]=1;

}

cout<<bfs(1)<<endl;

}
29 changes: 29 additions & 0 deletions CP/Assignment8-Graphs/Ankit Jain/friendless_dr_sheldon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>


using namespace std;


int main()

{
int x,a,b,t,y;

cin>>t;


while(t--)

{
cin>>a>>b;

for(int i=0;i<b;i++)

cin>>x>>y;

cout<<a-1<<endl;

}


}
Loading