Sunday, January 24, 2010

Snowfall

J&K is suffering from a heavily
snowy winter.The heavy snow even causes the closure of an important highway
connecting J&K and rest of India. You've got several reports containing the
start and end points of highway segments covered by heavy snow. Given those
reports , you are to return the total length of highway segments covered by
snow. Note that the reported segments may overlap.



Input



Input consists of T(T<=100)
number of test cases. T cases follow. Each input case starts with a number N ,
the number of road segments covered by snow. Next line contains N numbers , the
start points of the segment. Next line contains N numbers , the end points of
the segment. The points will always be <= 10000. For any particular segment
start point will always be less than the end point.



Output



For each test case print case number
followed by the total length of highway segments covered by snow. Suppose ur
output for 1st test case is 25 , then print "Case 1:25".



Sample Input















2
3
17
85 57
33
86 84
8
45
100 125 10 15 35 30 9
46
200 175 20 25 45 40 10



Sample Output





Case
1:44
Case
2:132















































































































































//  Snowfall.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


int _tmain(int argc, _TCHAR* argv[])


int main()
{
                int
T,N,A[100][100],temp1,temp2,i,j,test,dist,s,e;
                cin>>T;  
       
        test=0;
                while(test<T)
                {
                                dist=0;
                                cin>>N;
                                for(i=0;i<2;i++)
                               {
                                                for(j=0;j<N;j++)
                       
                        {
                                                                cin>>A[i][j];
                      
                        }
                                }
                                for(i=0;i<N;i++)
                                {
                                                for(j=0;j<=N-i;j++)
                       
                        {
                                                                if(A[0][j]>A[0][j+1]
&& (j+1)<N)
                                                                {
                                                                                temp1=A[0][j];
                                                                                A[0][j]=A[0][j+1];                          
          
                                                                     A[0][j+1]=temp1;
                                                                                temp2=A[1][j];
                                                                                A[1][j]=A[1][j+1];
                                                                                A[1][j+1]=temp2;
                                                                }
                       
                        }
                                }
                                /*for(i=0;i<2;i++)
                                {
                                                for(j=0;j<N;j++)
                       
                        {
                            
                                    printf("
%d",A[i][j]);
                       
                        }
                                                printf("\n");
                                }*/
                                s=A[0][0];e=A[1][0];dist=0;i=0;
                                for(j=1;j<N;j++)
                                {
                                                if(A[i][j]>e)
                       
                        {
                             
                                  dist=dist+(e-s);
                                                                s=A[i][j];
                                                                e=A[i+1][j];
                       
                        }
                       
                        else
if(A[i][j]<e && A[i+1][j]<e)
                       
                        {
                           
                                    if(j!=N-1)
                       
                                        {continue;}
                       
                        }
                       
                        else
if(A[i][j]<=e && A[i+1][j]>e)
                       
                        {
                          
                                     e=A[i+1][j];
                       
                        }
                       
                        if(j==N-1)
                       
                        {
                           
                                    dist=dist+(e-s);
                       
                        }
                                }                                                                                                                                        
 
                               cout<<"Case
"<<test+1<<":"<<dist<<endl;
                                test++;
                }
                //scanf("%d",&i);
                return 0;
}

2
3
17 85 57
33 86 84
8
45 100 125 10 15 35 30 9
46 200 175 20 25 45 40 10

Case 1:44
Case 2:132                                         




No comments:

Post a Comment