Professional Documents
Culture Documents
Samsung
Samsung
Samsung
#include<iostream>
using namespace std;
struct node{
int data;
struct node *next;
};
int main(){
int t;
cin >> t;
while(t--){
int n, e;
cin >> n >> e;
bool vis[n];
for(int i=0; i<n; i++)
vis[i]=false;
dfs(a,vis,0);
cout << endl;
}
}
void push(struct qnode **head, struct qnode **end, struct Node *data){
struct qnode *temp=(struct qnode *)malloc(sizeof(struct qnode));
if(*head==NULL){
*head=temp;
temp->data=data;
temp->next=NULL;
*end=*head;
}else{
(*end)->next=temp;
*end=temp;
temp->data=data;
temp->next=NULL;
}
}
push(&q,&end,node);
while(q){
struct Node *temp=front(q);
pop(&q);
if(temp->left)
push(&q,&end,temp->left);
if(temp->right)
push(&q,&end,temp->right);
}
}
int dist[V];
for(int i=0; i<V; i++)
dist[i]=1000000;
dist[src]=0;
for(int i=0; i<V; i++){
int u=minInd(dist,vis,V);
vis[u]=true;
for(int v=0; v<V; v++){
if(graph[u][v] && !vis[v] && dist[v]>dist[u]+graph[u][v])
dist[v]=dist[u]+graph[u][v];
}
}
#include<iostream>
using namespace std;
#define MAX 21
struct node{
int sx;
int sy;
struct node *next;
};
void push(struct node **head, struct node **end, int sx, int sy){
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->sx=sx;
temp->sy=sy;
temp->next=NULL;
if(*head==NULL){
*head=temp;
*end=temp;
}else{
(*end)->next=temp;
*end=temp;
}
}
int bfs(int a[MAX][MAX], int sx, int sy, int dx, int dy, int n){
bool vis[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
vis[i][j]=false;
vis[sx][sy]=true;
push(&q,&end,sx,sy);
while(q){
int x=q->sx;
int y=q->sy;
pop(&q);
int row[]={1,-1,0,0};
int col[]={0,0,1,-1};
for(int i=0; i<4; i++){
int u=x+row[i];
int v=y+col[i];
if(isValid(u,v,n) && !vis[u][v] && (a[u][v]==3 || a[u][v]==2)){
vis[u][v]=true;
push(&q,&end,u,v);
}
}
}
return 0;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[MAX][MAX];
int sx, sy, dx, dy;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> a[i][j];
if(a[i][j]==1){
sx=i;
sy=j;
}else if(a[i][j]==2){
dx=i;
dy=j;
}
}
}
}
return 0;
}
Topological Sort
struct node{
int data;
struct node *next;
};
void dfs(vector<int> a[], struct node **s, bool *vis, int src){
vis[src]=true;
vector<int> :: iterator i;
for(i=a[src].begin(); i!=a[src].end(); i++)
if(!vis[*i])
dfs(a,s,vis,*i);
push(s,src);
}
while(carry){
res[size]=carry%10;
carry=carry/10;
size++;
}
return size;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int res[3000];
res[0]=1;
int size=1;
for(int i=2; i<=n; i++)
size=multiply(res,i,size);
void push(struct node **head, struct node **end, struct Node *data){
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
if(*head==NULL){
*head=temp;
*end=temp;
}else{
(*end)->next=temp;
*end=temp;
}
}
int flag=1;
while(q){
struct Node *temp=q->data;
pop(&q);
if(temp==NULL){
if(q)
push(&q,&end,NULL);
flag=1;
}else{
if(flag==1){
cout << temp->data << " ";
flag=0;
}
if(temp->left)
push(&q,&end,temp->left);
if(temp->right)
push(&q,&end,temp->right);
}
}
}
Minimum Cost Path (dijktras using priority queue)
#include<iostream>
using namespace std;
struct node{
int cost;
int x;
int y;
};
struct heap{
int size;
struct node a[1000];
};
return q;
}
int min=i;
if(left!=-1 && (q->a[i]).cost>(q->a[left]).cost)
min=left;
if(right!=-1 && (q->a[min]).cost>(q->a[right]).cost)
min=right;
if(min!=i){
struct node temp=q->a[i];
q->a[i]=q->a[min];
q->a[min]=temp;
heapify(q,min);
}
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n][n];
bool vis[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++){
cin >> a[i][j];
vis[i][j]=false;
}
int ans=0;
struct heap *q=create(1,a[0][0],0,0);
while(q){
struct node temp=extractmin(q);
int cost=temp.cost;
int x=temp.x;
int y=temp.y;
if(vis[x][y])
continue;
vis[x][y]=true;
if(x==n-1 && y==n-1){
ans=cost;
break;
}
int row[]={1,-1,0,0};
int col[]={0,0,1,-1};
for(int i=0; i<4; i++){
int u=x+row[i];
int v=y+col[i];
if(isvalid(u,v,n) && !vis[u][v]){
insert(q,cost+a[u][v],u,v);
}
}
}
if(!root->right)
return root;
return leftmax(root->right);
}
Node *del(struct Node *root, int x){
if(root==NULL)
return NULL;
if(x==root->data){
if(!root->left && !root->right){
return NULL;
}else if(root->left && root->right){
struct Node *temp=leftmax(root->left);
root->data=temp->data;
root->left=del(root->left,temp->data);
return root;
}else if(root->left){
return root->left;
}else if(root->right){
return root->right;
}
}
root->left=del(root->left,x);
root->right=del(root->right,x);
return root;
}
if(k==0){
cout << root->data << " ";
return;
}
printkdistanceNodeDown(root->left,k-1);
printkdistanceNodeDown(root->right,k-1);
}
if(root==target){
flag=1;
printkdistanceNodeDown(target,k);
return;
}
if(flag==0)
print(root->left,target,k,flag);
if(flag==1){
k--;
if(k==0)
cout << root->data << " ";
else
printkdistanceNodeDown(root->right,k-1);
return;
}
if(flag==0)
print(root->right,target,k,flag);
if(flag==1){
k--;
if(k==0)
cout << root->data << " ";
else
printkdistanceNodeDown(root->left,k-1);
return;
}
}
void push(struct que **head, struct que **end, int data, struct Node
*qnode){
struct que *temp=(struct que *)malloc(sizeof(struct que));
temp->data=data;
temp->qnode=qnode;
temp->next=NULL;
if(*head==NULL){
*head=temp;
*end=temp;
}else{
(*end)->next=temp;
*end=temp;
}
}
if(temp->left)
push(&q,&end,dist-1,temp->left);
if(temp->right)
push(&q,&end,dist+1,temp->right);
}
Manacher Algorithm
#include<iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
string str;
cin >> str;
string s="$";
for(int i=0; str[i]!='\0'; i++)
s=s+"#"+str[i];
s=s+"#@";
int p[s.length()]={0};
int c=0, r=0;
for(int i=0; i<s.length(); i++){
int mirr=2*c-i;
if(i<r)
p[i]=min(p[mirr],r-i);
while(s[i+(p[i]+1)]==s[i-(p[i]+1)])
p[i]++;
if(i+p[i]>r){
c=i;
r=p[i]+i;
}
}
string ans="";
for(int i=ind-max+1; i<ind; i=i+2)
ans+=s[i];
int x=0;
if(max%2==0)
x=1;
for(int i=ind+x; i<=ind+max-1; i=i+2)
ans=ans+s[i];
Largest BST
struct node{
bool is;
int size;
int max;
int min;
};
if(!root->right)
info.max=root->data;
else
info.max=right.max;
return info;
}
return {false,1,-10000000,10000000};
int max=1;
largest(root,max);
return max;
}
No. of Turns
struct Node *lca(Node *root, int first, int second){
if(root==NULL)
return NULL;
if(root->key==first || root->key==second)
return root;
return left?left:right;
}
void calc(struct Node *root, int first, int &turn, int cnt, int side){
if(root==NULL)
return;
if(root->key==first){
turn=cnt;
return;
}
if(side==0){
calc(root->left,first,turn,0,1);
calc(root->right,first,turn,0,2);
}else if(side==1){
calc(root->left,first,turn,cnt,1);
calc(root->right,first,turn,cnt+1,2);
}else if(side==2){
calc(root->left,first,turn,cnt+1,1);
calc(root->right,first,turn,cnt,2);
}
}
int left=0;
calc(com,first,left,0,0);
int right=0;
calc(com,second,right,0,0);
if(com->key==first || com->key==second)
return left+right;
return left+right+1;
}
Huff Encoding
#include<bits/stdc++.h>
using namespace std;
struct node{
int freq;
char ch;
struct node *left;
struct node *right;
};
struct cmp{
bool operator()( node *a, node *b) {
return a->freq>b->freq;
}
};
preorder(root->left,s+"0");
preorder(root->right,s+"1");
}
int main(){
int t;
cin >> t;
while(t--){
string s;
cin >> s;
preorder(huff,"");
cout << endl;
}
return 0;
}
printbottom(root->left);
printbottom(root->right);
}
printright(root->right,1);
if(!root->right && flag==1)
printright(root->left,1);
if(flag==1)
cout << root->data << " ";
}
void print(int n, int from, int to, int aux, int &cnt){
if(n==1){
cout << "move disk 1 from rod " << from << " to rod " << to
<<endl;
cnt++;
return;
}
print(n-1,from,aux,to,cnt);
cout << "move disk " << n << " from rod " << from << " to rod " << to
<< endl;
cnt++;
print(n-1,aux,to,from,cnt);
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int cnt=0;
print(n,1,3,2,cnt);
cout << cnt << endl;
}
return 0;
}
int a[s1.length()+s2.length()]={0};
int n1=0, n2=0;
for(int i=s1.length()-1; i>=x; i--){
n2=0;
int carry=0;
for(int j=s2.length()-1; j>=y; j--){
int num=((s1[i]-'0')*(s2[j]-'0'))+a[n1+n2]+carry;
a[n1+n2]=num%10;
carry=num/10;
n2++;
}
if(carry>0)
a[n1+n2]=a[n1+n2]+carry;
n1++;
}
int i=s1.length()+s2.length()-1;
while(i>=0 && a[i]==0)
i--;
if(i==-1)
return "0";
string s="";
while(i>=0){
s=s+to_string(a[i]);
i--;
}
if((x==1&&y!=1)||(x!=1&&y==1))
s='-'+s;
return s;
}
if(a[l]>=a[mid])
return findPoint(a,l,mid-1);
return findPoint(a,mid+1,h);
}
int left=binarySearch(a,0,point,x);
int right=binarySearch2(a,point+1,n-1,x);
int main()
{
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int x;
cin >> x;
LRU Cache
struct node{
int key;
int value;
struct node *next;
};
struct que{
int size;
struct node *head;
struct node *end;
};
LRUCache::LRUCache(int N){
cnt=0;
temp->size=N;
temp->head=NULL;
temp->end=NULL;
}
int flag=0;
while(t){
if(t->key==x){
flag=1;
break;
}
prev=t;
t=t->next;
}
if(flag==1){
if(prev==NULL){
struct node *a=t;
temp->head=temp->head->next;
free(a);
}else{
prev->next=t->next;
if(prev->next==NULL)
temp->end=prev;
free(t);
}
int flag=0;
while(t){
if(t->key==x){
flag=1;
break;
}
prev=t;
t=t->next;
}
if(flag==0)
return -1;
int value=t->value;
if(prev==NULL){
struct node *a=temp->head;
temp->head=temp->head->next;
free(a);
}else{
prev->next=t->next;
if(prev->next==NULL)
temp->end=prev;
free(t);
}
https://github.com/ifqthenp/big-java/blob/master/late-objects/06-arrays-
and-array-lists/src/main/java/p_06_16_occupy_stalls/OccupyStalls.java
Endoscope Problem
#include<bits/stdc++.h>
using namespace std;
#define MAX 1001
struct node{
int r;
int c;
int cnt;
};
queue<struct node> q;
q.push({r,c,1});
while(!q.empty()){
struct node temp=q.front();
q.pop();
int x=temp.r;
int y=temp.c;
int cnt=temp.cnt;
if(cnt==len)
continue;
int p=a[x][y];
vector<pair<int,int>> :: iterator i;
for(i=dir[p].begin(); i!=dir[p].end(); i++){
int u=x+(*i).first;
int v=y+(*i).second;
if(isvalid(u,v,n,m) && !vis[u][v]){
if((*i).first==0 && (*i).second==1 && (a[u][v]==1 ||
a[u][v]==3 || a[u][v]==6 || a[u][v]==7)){
total++;
vis[u][v]=true;
q.push({u,v,cnt+1});
}else if((*i).first==0 && (*i).second==-1 && (a[u][v]==1 ||
a[u][v]==3 || a[u][v]==4 || a[u][v]==5)){
total++;
vis[u][v]=true;
q.push({u,v,cnt+1});
}else if((*i).first==1 && (*i).second==0 && (a[u][v]==1 ||
a[u][v]==2 || a[u][v]==4 || a[u][v]==7)){
total++;
vis[u][v]=true;
q.push({u,v,cnt+1});
}else if((*i).first==-1 && (*i).second==0 && (a[u][v]==1 ||
a[u][v]==2 || a[u][v]==5 || a[u][v]==6)){
total++;
vis[u][v]=true;
q.push({u,v,cnt+1});
}
}
}
}
}
int main(){
int t;
cin >> t;
while(t--){
int n, m, r, c, l;
cin >> n >> m >> r >> c >> l;
int a[MAX][MAX];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
cin >> a[i][j];
vector<vector<pair<int,int>>> dir={{{1,0},{-1,0},{0,1},{0,-1}},
{{1,0},{-1,0},{0,1},{0,-1}},
{{1,0},{-1,0}},
{{0,1},{0,-1}},
{{-1,0},{0,1}},
{{1,0},{0,1}},
{{0,-1},{1,0}},
{{-1,0},{0,-1}}};
int total=1;
if(a[r][c]==0)
total=0;
else
bfs(a,r,c,n,m,l,dir,total);
void space(int mat[MAX][5], int x, int y, int n, int bomb, int coin, int
&max){
if(x!=n){
if(mat[x][y]==2){
if(coin>max)
max=coin;
return;
}
if(mat[x][y]==1)
coin++;
}
if(isvalid(x-1,y-1,n))
space(mat,x-1,y-1,n,bomb,coin,max);
if(isvalid(x-1,y,n))
space(mat,x-1,y,n,bomb,coin,max);
if(isvalid(x-1,y+1,n))
space(mat,x-1,y+1,n,bomb,coin,max);
if(bomb==0){
int temp[n][5];
for(int i=0; i<n; i++){
for(int j=0; j<5; j++)
temp[i][j]=mat[i][j];
}
if(isvalid(x-1,y-1,n))
space(temp,x-1,y-1,n,1,coin,max);
if(isvalid(x-1,y,n))
space(temp,x-1,y,n,1,coin,max);
if(isvalid(x-1,y+1,n))
space(temp,x-1,y+1,n,1,coin,max);
}
if(coin>max)
max=coin;
return;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[MAX][5];
for(int i=0; i<n; i++)
for(int j=0; j<5; j++)
cin >> a[i][j];
int max=0;
space(a,n,2,n,0,0,max);
cout << max << endl;
}
return 0;
}
return ans;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
Infix to Postfix
#include<bits/stdc++.h>
using namespace std;
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
int main(){
int t;
cin >> t;
while(t--){
string s;
cin >> s;
int i=0;
stack<char> st;
while(i<s.length()){
if(s[i]=='(')
st.push(s[i]);
else if(s[i]==')'){
while(st.top()!='('){
cout << st.top();
st.pop();
}
st.pop();
}
else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' ||
s[i]=='^'){
if(st.empty())
st.push(s[i]);
else{
while(!st.empty() && prec(s[i])<=prec(st.top())){
cout << st.top();
st.pop();
}
st.push(s[i]);
}
}else
cout << s[i];
i++;
}
while(!st.empty()){
cout << st.top();
st.pop();
}
cout << endl;
}
return 0;
}
Median in a stream
#include<bits/stdc++.h>
using namespace std;
if(abs(lower.size()-higher.size())>=2){
if(lower.size()>higher.size()){
higher.push(lower.top());
lower.pop();
}else{
lower.push(higher.top());
higher.pop();
}
}
}
int main(){
int t;
cin >> t;
priority_queue<int> lower;
priority_queue<int,vector<int>,greater<int>> higher;
while(t--){
int num;
cin >> num;
addNumber(num,lower,higher);
rebalance(lower,higher);
cout << median(lower,higher) << endl;
}
return 0;
}
TSP using DP (Bitmasking)
#include<iostream>
using namespace std;
#define MAX 15
#define M 6000
long int tsp(int a[MAX][MAX], long int dp[M][MAX], long int mask, int pos,
int n){
if(mask==total){
if(a[pos][0]==0)
return 10000000;
return a[pos][0];
}
if(dp[mask][pos]!=-1)
return dp[mask][pos];
return dp[mask][pos]=ans;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[MAX][MAX];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
cin >> a[i][j];
total=(1<<n)-1;
cout << tsp(a,dp,1,0,n) << endl;
}
return 0;
}
Place the Cows
You are given an array of integers which represents positions available and
an integer c(cows).
Now you have to choose c positions such that minimum difference between
cows is maximized.
#include<iostream>
using namespace std;
if(elements==k)
return true;
}
}
return false;
}
if(feasible(a,high,mid,k)){
if(mid>res)
res=mid;
left=mid+1;
}else
right=mid;
}
return res;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n];
for(int i=0; i<n; i++)
cin >> a[i];
int k;
cin >> k;
quicksort(a,0,n-1);
Mr Kim Refrigerators
is going to visit all the customers and then return to his home. Each
location of the office, his home, and the customers is given in the form of
integer coordinates (x,y) (0≤x≤100, 0≤y≤100) . The distance between two
arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|,
where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The
locations of the office, his home, and the customers are all distinct. You
should plan an optimal way to visit all the N customers and return to his
among all the possibilities.
You are given the locations of the office, Mr. Kim’s home, and the
customers; the number of the customers is in the range of 5 to 10. Write a
program that, starting at the office, finds a (the) shortest path visiting
all the customers and returning to his home. Your program only have to
report the distance of a (the) shortest path.
Constraints
Input
You are given 10 test cases. Each test case consists of two lines; the
first line has N, the number of the customers, and the following line
enumerates the locations of the office, Mr. Kim’s home, and the customers
in sequence. Each location consists of the coordinates (x,y), which is
reprensented by ‘x y’.
Output
Input (20 lines in total. In the first test case, the locations of the
office and the home are (0, 0) and (100, 100) respectively, and the
locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50,
20).)
0 0 100 100 70 40 30 10 10 5 90 70 50 20
88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36
#1 200
#2 304
#3 366
#include<iostream>
using namespace std;
#define MAX 15
struct node{
int x;
int y;
};
void tsp(int a[MAX][MAX], bool *vis, int s, int v, int n, int c, int
&cost){
if(v==n){
if(c+a[s][1]<cost)
cost=c+a[s][1];
return;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[MAX][MAX];
for(int i=0; i<n+2; i++){
for(int j=0; j<n+2; j++){
if(i==j)
a[i][j]=0;
else
a[i][j]=abs(inp[i].x-inp[j].x)+abs(inp[i].y-inp[j].y);
}
}
bool vis[n+2];
for(int i=0; i<n+2; i++)
vis[i]=false;
int cost=100000000;
tsp(a,vis,0,0,n,0,cost);
cout << cost << endl;
}
}
Convex HULL
#include<iostream>
using namespace std;
struct point{
int x;
int y;
};
return y2*x1-y1*x2;
}
return x1*x1+y1*y1-x2*x2+y2*y2;
}
bool compare(point a, point b){
return (a.x==b.x && a.y==b.y);
}
int val=crossPro(current,next,points[i]);
if(val<0){
next=points[i];
co=0;
col=(struct point *)malloc(sizeof(struct point)*n);
}else if(val==0){
if(distance(current,next,points[i])<0){
col[co++]=next;
next=points[i];
}else{
col[co++]=points[i];
}
}
}
if(compare(next,start))
break;
result[cnt++]=next;
current=next;
}
return result;
}
return j;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
quicksort(Points,0,n-1);
points[x++]=Points[0];
struct point prev=points[0];
for(int i=1; i<n; i++){
if(!compare(Points[i],prev)){
points[x++]=Points[i];
prev=Points[i];
}
}
if(x<3){
cout << "-1" << endl;
continue;
}
int cnt=0;
struct point *result=findConvexHull(points,x,cnt);
quicksort(result,0,cnt-1);
Print Loop
Given a graph. We have to find the loop in the graph if it exist and
print the nodes of the loop in sorted order.
#include<bits/stdc++.h>
using namespace std;
#define MAX 20
bool cycle(int a[MAX][MAX], bool *vis, bool *rec, int s, int n, int &cnt,
int *loop, int &cause, int &fla){
rec[s]=true;
vis[s]=true;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[MAX][MAX];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
cin >> a[i][j];
int loop[n];
if(flag==1){
sort(loop,loop+cnt);
for(int i=0; i<cnt; i++)
cout << loop[i] << " ";
cout << endl;
}
if(flag==0)
cout << "-1" << endl;
}
}
struct point{
int x;
int y;
};
int main(){
int t;
cin >> t;
while(t--){
int sx, sy, dx, dy;
cin >> sx >> sy >> dx >> dy;
int n;
cin >> n;
int a[2*n+2][2*n+2];
for(int i=0; i<2*n+2; i++)
for(int j=0; j<2*n+2; j++)
a[i][j]=10000000;
points[i].x=x1;
points[i].y=y1;
points[i+1].x=x2;
points[i+1].y=y2;
a[i][i+1]=cost;
a[i+1][i]=cost;
}
n=2*n+2;
//Floyd Warshall
int dist[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
dist[i][j]=a[i][j];
struct bruh{
int big;
int com;
};
int main(){
int t;
cin >> t;
while(t--){
int n, k;
cin >> n >> k;
int a[n];
for(int i=0; i<n; i++)
cin >> a[i];
sort(a,a+n);
int sum[n];
sum[n-1]=a[n-1];
for(int i=n-2; i>=0; i--){
sum[i]=sum[i+1]+a[i];
}
int min=10000000;
for(int i=n-1; i>=k-1; i--){
int x=(info[i].big+info[i].com+1);
int stones=x*a[i];
if(i-(k-info[i].com-1)>=0)
stones+=sum[i-(k-info[i].com-1)]-sum[i];
if(stones<min)
min=stones;
}
cout << min << endl;
}
}
For Input:
1
12 5
1 2 2 3 3 3 3 3 5 9 9 11
Your Output is:
37
38
38
32
27
27
27
27
27
int a[n+2];
a[0]=1;
a[n+1]=1;
for(int i=1; i<n+1; i++)
a[i]=nums[i-1];
int dp[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
dp[i][j]=0;
return dp[0][n-1];
}
};
void dfs(int a[MAX][MAX], bool vis[MAX][MAX], int sx, int sy, int n, int
cnt, int &max){
if(sx==n-1 && sy==n-1){
if(cnt>max)
max=cnt;
return;
}
int row[]={0,0,1,-1};
int col[]={1,-1,0,0};
for(int i=0; i<4; i++){
int u=sx+row[i];
int v=sy+col[i];
if(isvalid(u,v,n) && !vis[u][v] && a[u][v]!=1){
vis[u][v]=true;
if(a[u][v]==2)
dfs(a,vis,u,v,n,cnt+1,max);
else
dfs(a,vis,u,v,n,cnt,max);
vis[u][v]=false;
}
}
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[MAX][MAX];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
cin >> a[i][j];
}
bool vis[MAX][MAX];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
vis[i][j]=false;
int max=0;
dfs(a,vis,0,0,n,0,max);
long long int func(long long int a, long long int b, long long int c, long
long int n){
long long int A=a*n;
long long int logValue=floor(log2(n));
long long int B=b*n*logValue;
long long int C=c*n*n*n;
return A+B+C;
}
long long int position(long long int a, long long int b, long long int c,
long long int k){
long long int start=0, end=SMALL;
if(c==0)
end=LARGE;
int main(){
int t;
cin >> t;
while(t--){
long long int a, b, c;
cin >> a >> b>> c;
Alien Dictionary
void topo(int a[MAX][MAX], stack<int> &st, bool *vis, int s){
vis[s]=true;
bool vis[26];
for(int i=0; i<26; i++)
vis[i]=true;
stack<int> s;
for(int i=0; i<26; i++){
if(!vis[i])
topo(a,s,vis,i);
}
string str="";
while(!s.empty()){
str=str+(char)(s.top()+'a');
s.pop();
}
return str;
}
string str="";
while(!st.empty()){
str=st.top()+str;
st.pop();
}
return str;
}
int main(){
int t;
cin >> t;
while(t--){
string s;
cin >> s;
if(s.length()<2){
cout << s << endl;
continue;
}
while(1){
int n=s.length();
s=rem(s);
if(n==s.length())
break;
}
struct node{
string str;
int len;
bool op;
struct node *next;
};
void push(struct node **head, struct node **en, string str, int len, bool
op){
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->str=str;
temp->len=len;
temp->op=op;
temp->next=NULL;
if(*head==NULL){
*head=temp;
*en=temp;
}else{
(*en)->next=temp;
*en=temp;
}
}
num2=0;
flag=1;
}
operand=s[i];
flag=1;
}else{
if(flag==0)
num1=num1*10+(s[i]-'0');
else if(flag==1)
num2=num2*10+(s[i]-'0');
}
}
if(flag==1){
if(operand=='+')
num1=num1+num2;
else if(operand=='-')
num1=num1-num2;
else if(operand=='*')
num1=num1*num2;
else if(operand=='/' && num2!=0)
num1=num1/num2;
}
return num1;
}
string f="";
for(int i=0; i<N; i++)
push(&q,&en,f+s[i],1,false);
while(q){
string str=q->str;
int len=q->len;
bool op=q->op;
pop(&q);
if(len==o)
continue;
int n=str.length()-1;
for(int i=0; i<s.length(); i++){
if(str[n]=='+' || str[n]=='-' || str[n]=='*' || str[n]=='/'){
if(!(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/'))
push(&q,&en,str+s[i],len+1,op);
}else{
if(s[i]=='+' || s[i]=='-' || s[i]=='*' ||
s[i]=='/')
push(&q,&en,str+s[i],len+1,true);
else
push(&q,&en,str+s[i],len+1,op);
}
}
}
}
int main(){
int t;
cin >> t;
while(t--){
int n, m, o;
cin >> n >> m >> o;
string s="";
for(int i=0; i<n; i++){
char ch;
cin >> ch;
s=s+ch;
}
int num;
cin >> num;
if(n==9){
int dig=0;
for(int i=num; i>0; i=num/10)
dig++;
if(dig>o)
cout << "-1" << endl;
else
cout << dig << endl;
continue;
}
bool flag=false;
mini=-1;
bfs(s,num,o,n,flag);
if(flag)
mini++;
Ford Flukerson
#include<bits/stdc++.h>
using namespace std;
#define MAX 1005
bool bfs(int res[MAX][MAX], int source, int sink, int *parent, int n){
bool vis[n];
for(int i=0; i<n; i++)
vis[i]=false;
queue<int> q;
q.push(source);
vis[source]=true;
parent[source]=-1;
while(!q.empty()){
int u=q.front();
q.pop();
return (vis[sink]==true);
}
int parent[n];
int maxflow=0;
while(bfs(res,source,sink,parent,n)){
int pathflow=INT_MAX;
int u, v;
for(v=sink; v!=source; v=parent[v]){
u=parent[v];
pathflow=min(pathflow,res[u][v]);
}
maxflow+=pathflow;
}
return maxflow;
}
int main(){
int t;
cin >> t;
while(t--){
int n, m;
cin >> n >> m;
int a[MAX][MAX];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j]=0;
void bruh(vector<int> &car, int gas, int des, int fuel, int pos, int n, int
dist, int &mdist){
if(gas==0 && des==0){
if(dist<mdist)
mdist=dist;
return;
}
if(pos==-1 || pos==n){
if(pos==-1){
if(fuel==2 || gas==0)
return;
fuel=-2;
bruh(car,gas,des,fuel,pos+1,n,dist+1,mdist);
}else if(pos==n){
if(fuel==-2 || des==0)
return;
fuel=2;
bruh(car,gas,des,fuel,pos-1,n,dist+1,mdist);
}
}else{
if(fuel>0){
bruh(car,gas,des,fuel,pos-1,n,dist+1,mdist);
if(car[pos]==2){
car[pos]=0;
bruh(car,gas,des-1,0,n,n,dist+n-pos,mdist);
bruh(car,gas,des-1,fuel-1,pos-1,n,dist+1,mdist);
car[pos]=2;
}
}else if(fuel<0){
bruh(car,gas,des,fuel,pos+1,n,dist+1,mdist);
if(car[pos]==1){
car[pos]=0;
bruh(car,gas-1,des,0,-1,n,dist+pos+1,mdist);
bruh(car,gas-1,des,fuel+1,pos+1,n,dist+1,mdist);
car[pos]=1;
}
}else if(fuel==0){
if(gas)
bruh(car,gas,des,0,-1,n,dist+pos+1,mdist);
if(des)
bruh(car,gas,des,0,n,n,dist+n-pos,mdist);
}
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> car;
int gas=0, des=0;
for(int i=0; i<n; i++){
int a;
cin >> a;
car.push_back(a);
if(a==1)
gas++;
else
des++;
}
int mdist=INT_MAX;
bruh(car,gas,des,0,-1,n,0,mdist);
Construction Basestation
#include<bits/stdc++.h>
using namespace std;
#define X 20
void bruh(int a[X][X], bool vis[X][X], int u, int v, int sum, int depth,
int n, int m, int &max, vector<pair<int,pair<int,int>>> vec){
if(v%2==0){
int row[]={-1,-1,-1,0,0,0,1};
int col[]={-1,0,1,-1,0,1,0};
for(int i=0; i<7; i++){
int x=u+row[i];
int y=v+col[i];
if(depth==3){
sort(vec.begin(),vec.end());
for(int i=vec.size()-1; i>=0; i--){
if(!vis[vec[i].second.first][vec[i].second.second]){
sum+=vec[i].first;
break;
}
}
if(sum>max)
max=sum;
return;
}
if(v%2==0){
int row[]={-1,-1,-1,0,0,0,1};
int col[]={-1,0,1,-1,0,1,0};
int main(){
int t;
cin >> t;
while(t--){
int n, m;
cin >> n >> m ;
int a[X][X];
bool vis[X][X];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> a[i][j];
vis[i][j]=false;
}
}
TollGate
#include<iostream>
using namespace std;
void bruh(int *men, int *cost, int pos, int b3, int b2, int b1, int n, int
sum, int &t_cost){
if(sum>=t_cost)
return;
if(pos==n){
if(sum<t_cost)
t_cost=sum;
return;
}
bruh(men,cost,pos+1,b3,b2,b1,n,sum+cost[pos],t_cost);
bruh(men,cost,pos+1,b3+men[pos],b2,b1,n,sum+2*cost[pos],t_cost);
int pep=b3+b2+b1;
if(pep>=men[pos]){
if(men[pos]>b2+b1){
b3=pep-men[pos];
b2=0;
b1=0;
}else if(men[pos]>b1){
b2=b2+b1-men[pos];
b1=0;
}
bruh(men,cost,pos+1,0,b3,b2,n,sum,t_cost);
}
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int t_cost=1000000;
bruh(men,cost,0,0,0,0,n,0,t_cost);
struct node{
int x;
int y;
int dist;
struct node *next;
};
void push(struct node **head, struct node **end, int x, int y, int dist){
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->x=x;
temp->y=y;
temp->dist=dist;
temp->next=NULL;
if(*head==NULL){
*head=temp;
*end=*head;
}else{
(*end)->next=temp;
*end=temp;
}
void bfs(int a[X][X], int vis[X][X], int dist[X][X], int x_cord, int
y_cord, int d, int re, int n){
struct node *head=NULL;
struct node *end=head;
push(&head,&end,x_cord,y_cord,d);
while(head){
struct node *temp=head;
head=head->next;
int u=temp->x;
int v=temp->y;
int dis=temp->dist;
int row[]={1,0,-1,0};
int col[]={0,-1,0,1};
int main(){
int t;
cin >> t;
while(t--){
int n, rare;
cin >> n >> rare;
int elements[rare][2];
for(int i=0; i<rare; i++){
int x, y;
cin >> x >> y;
elements[i][0]=x-1;
elements[i][1]=y-1;
}
int a[X][X];
int vis[X][X];
int dist[X][X];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> a[i][j];
vis[i][j]=0;
dist[i][j]=0;
}
}
int ans=100000;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(vis[i][j]==rare){
if(dist[i][j]<ans)
ans=dist[i][j];
}
}
}
Bipartite
Given a graph print either of the set of the vertices that are colored with the same color. And
if the graph is not bipartite print “-1”. Test cases also included cases when a graph is not
connected.
return true;
}
while(t--){
int d, m, y;
cin >> d >> m >> y;
int year=y;
y--;
y=y%400;
//int a[]={5,3,1,0};
int odd=0;
if(y>=100 && y<200)
odd=5;
else if(y>=200 && y<300)
odd=3;
else if(y>=300)
odd=1;
y=y%100;
odd+=(y/4)*2 + (y-(y/4));
int month[]={3,0,3,2,3,2,3,3,2,3,2,3};
if(year%400==0 || ((year%100)!=0 && (year%4)==0))
month[1]=1;
odd=odd%7;
string day[]={"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
cout << day[odd] << endl;
}
return 0;
}
Mr Lee Travels
Mr. Lee has to travel various offices abroad to assist branches of each place. But he has a problem. The airfare
would be real high as all offices he has to visit are in foreign countries. He wants to visit every location only
one time and return home with the lowest expense. Help this company-caring man calculate the lowest
expense.
#include<iostream>
using namespace std;
#define X 20
void bruh(int mat[X][X], bool vis[], int s, int v, int n, int c, int
&cost){
if(v==n){
if(c+mat[s][0]<cost)
cost=c+mat[s][0];
return;
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int mat[X][X];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> mat[i][j];
}
}
bool vis[n];
for(int i=0; i<n; i++)
vis[i]=false;
int cost=1000000;
bruh(mat,vis,0,1,n,0,cost);
cout << cost << endl;
}
return 0;
}
Toggling
#include<iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n, m, k;
cin >> n >> m >> k;
int a[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++)
cin >> a[i][j];
}
int dp[1048576]={0};
for(int i=0; i<n; i++){
int num=0;
for(int j=m-1; j>=0; j--){
num=num*2;
if(a[i][j]==1)
num=num+1;
}
dp[num]++;
}
int max=-1;
for(int i=0; i<1048576; i++){
if(dp[i]>0 && dp[i]>max){
int unset=0;
int temp=i;
while(temp>0){
int a=temp&1;
if(a==0)
unset++;
temp>>=1;
}
if(unset<=k && (k-unset)%2==0)
max=dp[i];
}
}