Search Here

Saturday, July 4, 2015

UVa - 12488 ( Start Grid Solution ) [ C & C++ Code ]

Try Yourself First. For help Scroll Down .

Clearance : This is an Array Manipulation Problem . All you have to do is to find the total number of OPERATIONS  needed to reform the array from the FIRST list to the SECOND list . On every OPERATIONS you have to do operate with newly formed array . Easy Problem . So now solve it .
Edit the First array as you have make the second array .
=> Algorithm :
===========
i)  Find the i'th element of the Second List from the First List . Store the Location & the Number. Then Break.
ii) Then initiate a loop from that Location down to i'th index . Enter ( i - 1)'th element of the First List to the i'th index & increase the OPERATION number. 
iii) Then enter the NUMBER you stored at Step (i) into the i'th index .
iv) Print  the operation number .

Code :


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        int start[n+1], finish[n+1];
        for(int i=1; i<=n; i++)
            scanf("%d",&start[i]);
        for(int i=1; i<=n; i++)
            scanf("%d",&finish[i]);
        int ot = 0, loc, num;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(finish[i]==start[j])
                    {  loc = j; num = finish[i]; break;}
            }
            for(int j=loc; j>i; j--)
            {
                start[j]=start[j-1];
                ot++;
            }
            start[i]=num;
        }
        cout<<ot<<endl;
    }
    return 0;
}



=> Question or Need Help ?? Leave a Comment .

1 comment: