Monk’s Love for Food

QUESTION

Our monk loves food. Hence,he took up position of a manager at Sagar,a restaurant that serves people with delicious food packages. It is a very famous place and people are always queuing up to have one of those packages. Each package has a cost associated with it. The packages are kept as a pile. The job of a manager is very difficult. He needs to handle two types of queries:\n\n1) Customer Query:\nWhen a customer demands a package, the food package on the top of the pile is given and the customer is charged according to the cost of the package. This reduces the height of the pile by 1. \nIn case the pile is empty, the customer goes away empty-handed.\n\n2) Chef Query:\nThe chef prepares a food package and adds it on top of the pile. And reports the cost of the package to the Manager.\nHelp him manage the process.\n\nInput:\nFirst line contains an integer Q, the number of queries. Q lines follow.\nA Type-1 ( Customer) Query, is indicated by a single integer 1 in the line.\nA Type-2 ( Chef) Query, is indicated by two space separated integers 2 and C (cost of the package prepared) .\n\nOutput:\nFor each Type-1 Query, output the price that customer has to pay i.e. cost of the package given to the customer in a new line. If the pile is empty, print \”No Food\” (without the quotes).\n\nConstraints:\n1 Q 10^5\n1 C 10^7.

“TESTCASE_1”: “6\n1\n2 5\n2 7\n2 9\n1\n1\n###—###SEPERATOR—###—\nNo Food\n9\n7”, “TESTCASE_2”: “7\n2 7344\n2 83\n2 4314234\n1\n1\n2 16323\n2 483980\n###—###SEPERATOR—###—\n4314234\n83”, “TESTCASE_3”: “2\n1\n1\n###—###SEPERATOR—###—\nNo Food\nNo Food”, “TESTCASE_4”: “4\n1\n2 6926748\n1\n1\n###—###SEPERATOR—###—\nNo Food\n6926748\nNo Food”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<stdio.h>
int main()
{
	int q;
	int top = -1;
	int cost[100000];
	scanf("%d",&q);
	while(q--)
	{
		int type;
		scanf("%d",&type);
		if(type == 1)
		{
			if(top==-1)
			   printf("No Food\n");
			else
			{
				printf("%d\n",cost[top]);
			    top = top -1;
			}
		}
		if(type == 2)
		{
			int price;
			top = top +1;
			scanf("%d",&price);
			cost[top] = price;
		}
	}
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro