#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*Struct defining cube*/
typedef struct
{
int l;
int b;
int h;
}Cube;
/*Cube compare function */
int cube_compare(const void *x, const void *y)
{
const Cube *a = x;
const Cube *b = y;
if(a->l == b->l)
{
if(a->b == b->b)
return a->h - b->h;
return a->b - b->b;
}
return a->l - b->l;
}
/*Struct to hold successor info*/
typedef struct
{
int next;
int chainLength;
}Successor;
/*Checks whether cube 'a' fits into cube 'b' */
inline int cube_fits(Cube *a , Cube *b)
{
return (a->l < b-> l) && (a->b < b->b) && (a->h < b->h);
}
/*Prints all the cubes which fits into one another */
void max_fit_cubes(Cube cubes[], int n)
{
//Fill the chain
Successor nextCube[n];
memset(nextCube, 0, sizeof(nextCube));
int i = n - 1, j;
while(i--)
{
for(j = i + 1; j < n; j++)
{
if(cube_fits(cubes + i, cubes + j))
{
if((nextCube[j].chainLength + 1) > nextCube[i].chainLength)
{
nextCube[i].chainLength = nextCube[j].chainLength + 1;
nextCube[i].next = j;
}
}
}
}
//find the max chain
int m = i = n - 1;
while(i--)
{
if(nextCube[i].chainLength > nextCube[m].chainLength)
m = i;
}
//Now print the max chain
printf("Max Chain Length: %d, m: %d\n", nextCube[m].chainLength, m);
do
{
printf("(%d,%d,%d) =>", cubes[m].l, cubes[m].b, cubes[m].h);
m = nextCube[m].next;
}while(m);
printf("\n");
}
int main()
{
int n;
scanf("%d", &n);
Cube cubes[n];
int i;
for(i = 0; i < n; ++i)
{
scanf("%d%d%d", &(cubes[i].l), &(cubes[i].b), &(cubes[i].h));
}
qsort(cubes, n, sizeof(Cube), cube_compare);
max_fit_cubes(cubes, n);
return 0;
}
Monthly Archives: September 2010
Given a binary tree, and 3 nodes x,y,z write a function which returns true if y lies in the path between x and z and false otherwise
int find_y_in_xz_path(Tree t, Node *y, Node *z, int yfound)
{
if(!t)
return 0;
if(t == z)
{
return yfound;
}
else if(t == y)
{
yfound = 1;
}
if(find_y_in_xz_path(t->left, y, z, yfound))
return 1;
return find_y_in_xz_path(t->right, y, z, yfound);
}
int main()
{
find_y_in_xz_path(x,y,z,0);
}
Reverse a stack using standard stack operations like push(), pop(), isEmpty()
#include <stack>
#include <iostream>
using namespace std;
typedef stack<int> Stack;
struct Node
{
int d;
Node *prev;
};
void push(Stack &s, Node *p)
{
if(!p) return;
push(s, p->prev);
s.push(p->d);
}
void reverse(Stack &s, Node *p)
{
if(!s.empty())
{
Node n = {s.top(), p};
s.pop();
reverse(s, &n);
}
else
{
push(s, p);
}
}
int main()
{
Stack s;
int n;
cin >> n;
for(int i = 0; i < n; i++)
s.push(i);
reverse(s, NULL);
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
To find maximum and second maximum element in an Array using N+logN comparisons
#include <stdio.h>
typedef struct
{
unsigned a;
unsigned b;
}Max2ndMax;
Max2ndMax findMaxN2ndMax(int a[], int i, int j)
{
Max2ndMax ret;
int n = j - i;
if(n == 0)
{
ret.a = ret.b = a[i];
return ret;
}
else if(n == 1)
{
if(a[i] > a[j])
ret.a = a[i], ret.b = a[j];
else
ret.a = a[j], ret.b = a[i];
return ret;
}
int mid = (i + j)/2;
Max2ndMax ret1 = findMaxN2ndMax(a, i, mid);
Max2ndMax ret2 = findMaxN2ndMax(a, mid + 1, j);
if(ret1.a > ret2.a)
{
ret.a = ret1.a;
ret.b = ret2.a > ret1.b ? ret2.a : ret1.b != ret1.a ? ret1.b : ret2.a;
}
else
{
ret.a = ret2.a;
ret.b = ret1.a > ret2.b ? ret1.a : ret2.b != ret2.a ? ret2.b : ret1.a;
}
return ret;
}
int main()
{
int n;
scanf("%d", &n);
int a[n];
int i;
for(i = 0; i < n; ++i)
scanf("%d", a + i);
Max2ndMax m = findMaxN2ndMax(a, 0, n-1);
printf("Max = %d\t 2ndMax = %d\n", m.a, m.b);
}