Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Saturday, September 7, 2013

SPOJ:Cartman stoles the cookies and escapes solution !!

Question :
Cartman went to Stan's home tonight, but Stan's family had to go out immediately due to some important work. They left Cartman at their home and asked him to see the belongings till they come back.
But Cartman's intention's were totally different, as soon as Stan's family left, he entered into their kitchen. He saw that there were "N" cupboards in the kitchen. All of them had cookies in them. Cartman went to each cupboard and ate all the cookies. After eating the cookies he realized that he should not leave any clue behind him that he ate the cookies. Each cupboard had two doors, a left door and a right door. He remembered that when he came there all the left doors were in the same position (open or closed). Also the right doors were also in the same position (open or closed). Cartman realized that he should maintain this condition before the Stan's family arrives. But he didn't remember the initial position of the doors. So, he only tries to make all the left door in the same position and all the right doors in the same position. For example, all the left doors may be open and all the right doors may be closed. Cartman opens or closes a door in one second. He wants to achieve his goal in the minimum time. Write a program that tells the minimum time required to acheive this task.

Inputs :
First line of the input will contain a single integer "N", the no. of cupboards. Each of the next "N" lines contain two integers Li and Ri. Li equals 1 if the left door of the i-th cupboard is open, Li equals 0 if the left door of the i-th cupboard is closed. Similarly Ri is 1 if right door is open and 0 if right door is closed.

Outputs :
Print a single integer "T", the minimum no. of seconds Cartman needs to change the doors of all cupboards to the position he needs.

Constraints :
2 <= N <= 10^4, 0 <= Li, Ri <=1,





Level : Medium

Solution to the Problem :


#include<stdio.h>

int min(int i,int j)
{
    return (i<j)?i:j;
}
main()
{
      int n,i;
      scanf("%d",&n);
      int l[n];
      int r[n];
      for(i=0;i<n;i++)
      scanf("%d%d",&l[i],&r[i]);
      int c1,c2;
      c1=c2=0;
      for(i=0;i<n;i++)
      {
                      if(l[i]==0)
                      c1++;
                      if(r[i]==0)
                      c2++;
      }
      printf("%d\n",min(c1,n-c1)+min(c2,n-c2));
      
}



Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.