From 89aaab15ae64319d3b0e7d3d6a8cd3329962853c Mon Sep 17 00:00:00 2001 From: Pulkit Sambhavi Singh <49859368+Purukitto@users.noreply.github.com> Date: Mon, 7 Oct 2019 13:41:06 +0530 Subject: [PATCH 1/4] Update and rename ST4.cpp to ST4.c --- ST4.c | 35 +++++++++++++++++++++++++++++++++++ ST4.cpp | 51 --------------------------------------------------- 2 files changed, 35 insertions(+), 51 deletions(-) create mode 100644 ST4.c delete mode 100644 ST4.cpp diff --git a/ST4.c b/ST4.c new file mode 100644 index 0000000..0394921 --- /dev/null +++ b/ST4.c @@ -0,0 +1,35 @@ +#include +void calculateSpan(int price[], int n, int S[]) +{ +S[0] = 1; +int i; +for ( i = 1; i < n; i++) +{ +S[i] = 1; +int j; +for ( j = i-1; (j>=0)&&(price[i]>=price[j]); j--) +S[i]++; +} +} + +void printArray(int arr[],int n) +{ +int i; +for ( i = 0; i < n; i++) +printf("%d ", arr[i]); +} + +int main() +{ +int size,i,q,price[100]; +scanf("%d",&size); +for(i=0;i - -// Fills array S[] with span values -void calculateSpan(int price[], int n, int S[]) -{ -// Span value of first day is always 1 -S[0] = 1; - -// Calculate span value of remaining days by linearly checking -// previous days -int i; -for ( i = 1; i < n; i++) -{ -S[i] = 1; // Initialize span value - -// Traverse left while the next element on left is smaller -// than price[i] -int j; -for ( j = i-1; (j>=0)&&(price[i]>=price[j]); j--) -S[i]++; -} -} - -// A utility function to print elements of array -void printArray(int arr[],int n) -{ -int i; -for ( i = 0; i < n; i++) -printf("%d ", arr[i]); -} - -// Driver program to test above function -int main() -{ -int size,i,q,price[100]; -scanf("%d",&size); -for(i=0;i Date: Mon, 7 Oct 2019 13:43:52 +0530 Subject: [PATCH 2/4] Update and rename ST1.cpp to ST1.c --- ST1.cpp => ST1.c | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) rename ST1.cpp => ST1.c (59%) diff --git a/ST1.cpp b/ST1.c similarity index 59% rename from ST1.cpp rename to ST1.c index 5fddc2e..16bbeab 100644 --- a/ST1.cpp +++ b/ST1.c @@ -1,20 +1,17 @@ #include #include -// Stack is represented using linked list struct stack { int data; struct stack *next; }; -// Utility function to initialize stack void initStack(struct stack **s) { *s = NULL; } -// Utility function to chcek if stack is empty int isEmpty(struct stack *s) { if (s == NULL) @@ -22,7 +19,6 @@ return 1; return 0; } -// Utility function to push an item to stack void push(struct stack **s, int x) { struct stack *p = (struct stack *)malloc(sizeof(*p)); @@ -38,63 +34,43 @@ p->next = *s; *s = p; } -// Utility function to remove an item from stack int pop(struct stack **s) { int x; struct stack *temp; - x = (*s)->data; temp = *s; (*s) = (*s)->next; free(temp); - return x; } - -// Function to find top item int top(struct stack *s) { return (s->data); } -// Recursive function to insert an item x in sorted way void sortedInsert(struct stack **s, int x) { -// Base case: Either stack is empty or newly inserted -// item is greater than top (more than all existing) if (isEmpty(*s) || x > top(*s)) { push(s, x); return; } - -// If top is greater, remove the top item and recur int temp = pop(s); sortedInsert(s, x); - -// Put back the top item removed earlier push(s, temp); } -// Function to sort stack void sortStack(struct stack **s) { -// If stack is not empty if (!isEmpty(*s)) { -// Remove the top item int x = pop(s); - -// Sort remaining stack sortStack(s); - -// Push the top item back in sorted stack sortedInsert(s, x); } } -// Utility function to print contents of stack void printStack(struct stack *s) { while (s) @@ -105,7 +81,6 @@ s = s->next; printf("\n"); } -// Driver Program int main() { int n,i; @@ -113,21 +88,14 @@ scanf("%d",&n); int arr[n]; for(i=0;i Date: Mon, 7 Oct 2019 13:54:11 +0530 Subject: [PATCH 3/4] Create TR2.cpp --- TR2.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 TR2.cpp diff --git a/TR2.cpp b/TR2.cpp new file mode 100644 index 0000000..0039ef8 --- /dev/null +++ b/TR2.cpp @@ -0,0 +1,61 @@ + #include + using namespace std; +void MERG(int x,int y); +int FIND_root(int i); + int root(int arr[], int i) + { + + while(arr[i] != i) + { + i = arr[i]; + arr[i] = arr[arr[i]]; + + } + return i; + } + + void un(int arr[],int size[], int a, int b) + { + int pa = root(arr,a); + int pb = root(arr,b); + + if(pa==pb) return; + + if(size[pa] > size[pb]) + { + arr[pb] = pa; + size[pa] += size[pb]; + size[pb] = 0; + } + else + { + arr[pa] = arr[pb]; + size[pb] += size[pa]; + size[pa] =0; + } + } + int main() + { + int n,m; + cin>>n>>m; + int arr[n+1]; + int size[n+1]; + for(int i=1;i<=n;i++) + { + arr[i] = i; + size[i] = 1; + } + while(m--) + { + int x,y; + cin>>x>>y; + un(arr,size,x,y); + + } + for(int i=1;i<=n;i++) + { + cout< Date: Mon, 7 Oct 2019 13:59:32 +0530 Subject: [PATCH 4/4] Create TRE14.cpp --- TRE14.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 TRE14.cpp diff --git a/TRE14.cpp b/TRE14.cpp new file mode 100644 index 0000000..d0b2528 --- /dev/null +++ b/TRE14.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; +#define watch(x) cout << (#x) << " is " << (x) << "\n" +#define pow2(x) ((x)*(x)) +#define max3(a,b,c) max(a,max(b,c)) +#define min3(a,b,c) min(a,min(b,c)) +#define ll long long int +#define eb emplace_back +#define pb push_back +#define mod 1000000007 +#define mp make_pair +#define ff first +#define ss second +#define all(c) (c).begin(),(c).end() +#define nl "\n" +typedef vector vi; +typedef vector vl; +typedef vector< vi > vvi; +typedef vector< vl > vvl; +typedef pair< int,int > ii; +typedef pair< ll,ll> pll; +typedef map< ll,ll> mll; +typedef map< int,int> mii; +struct node *rightside; +struct node *leftside; +void inorder(ll ar[],vl &v,ll n,ll i) +{ + if(i>=n) + return; + inorder(ar,v,n,2*i+1); + v.eb(ar[i]); + inorder(ar,v,n,2*i+2); +} +ll minSwap(vl &v) +{ + vector v1(v.size()); + ll ans = 0ll; + ll n = v.size(); + for(int i=0;i> n; + ll ar[n]; + for(int i=0;i> ar[i]; + vl v; + inorder(ar,v,n,0); + cout << minSwap(v) << nl; + return 0; +}