Thursday 27 July 2017

SPOJ : EGYPIZZA Solution

Question EGYPIZZA - Pizza
Exaplaination :
Count the number of 1/4 , 1/2 , 3/4. 3/4 slice will have space for a 1/4 slice. Now,you can start from counting the number of 3/4, because a single can have just one slice of 3/4 and one of 1/4 at the same time.So , we count the number of 3/4slices, and the 1/4slices which can fit in each of them.Now,if there are extra 1/4slices, we add them up.Now,the fraction part of this sum tells what amount of slice is cut from last pizza. if we have some slice left(ie greater than equal to 1/2 of pizza),we can just have one 1/2slice from this pizza.Now, count the rest of 1/2 slices and account the no. of pizzas.


#include <iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;

int main() {
    int n;
    int half=0,one4=0,three4=0;
    scanf("%d",&n);//no. of slices

    char st[5];
    while(n--)
    {
     scanf(" %s",st);
     if(strcmp(st,"1/4")==0) one4++;
     if(strcmp(st,"1/2")==0) half++;
     if(strcmp(st,"3/4")==0) three4++;//counting no.of slices of each type
    }

    int sum=three4;//counting no.of 3/4 slices
    three4-=one4;//fitting them with 1/4 slices
    if(three4<0)//if its negative, means there still some 1/4 slices
      {
          double k=(-three4 * 1.0/4.0);//sum all left over 1/4 slices
          sum=sum+(int)k;//add no.of pizzas
          k=k-(int)k;//amount cut off last pizza,its <1
          k=1-k;//amount left in last pizza
          if(k>=0.5 && half>0)//if amount left is still >=0.5
            half--;//cut off a half from it
          sum++;//add no. of pizza
      }
    sum=sum+(ceil(half*0.5));//add rest of half slices
    sum=sum+1;//add one pizza of winner
    printf("%d\n",sum);
    return 0;
} 

No comments:

Post a Comment

SPOJ : MARBLES Solution

Question  MARBLES - Marbles Explaination.... Fill n spaces , with k colors. First select k spaces with k-different color.  Now we are l...