Find the number of Swaps required

QUESTION

Mommy is a very active lady. She likes to keep all stuff sorted. She has developed an interesting technique of sorting stuff over the years. She goes through the items repeatedly from first to last and whenever she finds two consecutive items unsorted, she puts them in the proper order. She continues the process until all the items are sorted. One day Mommy has to attend a wedding ceremony. Suddenly she remembers that she has not sorted the plates after washing. She has only M minutes left. If she can complete the task within the remaining time, she will sort her plates and then attend the wedding. However if she cannot, she decides to skip the task. She knows that she take S seconds per swap. However she does not know the total number of swaps required and hence she is in trouble. She wants you to help her out.\n\n\nInput:\nThe first line of input takes the number of test cases T .Then T test cases follow .\nEach test case contains 2 lines . The first line of each test case contains 3 space separated integers M,S and N, where N is the number of plates .The next line of the test case contains N space separated values which denotes the size of the plates .\n \n\nOutput:\n\nPrint 1 if mommy can complete the task, 0 otherwise.\n \n\nConstraints:\n\n1<=T<=100\n\n1<=M<=100\n\n1<=S<=100\n\n1<=N<=100\n\n1<=Size of Plate<=200.

“TESTCASE_1”: “3\n20 15 5\n35 10 85 90 30\n10 30 10\n48 14 37 29 30 47 11 23 25 8\n5 40 8\n25 28 12 20 6 5 37 26\n###—###SEPERATOR—###—\n1\n0\n0”, “TESTCASE_2”: “1\n10 10 10\n25 32 10 5 21 4 55 90 14 26\n###—###SEPERATOR—###—\n1”, “TESTCASE_3”: “3\n5 40 5\n2 5 1 4 0\n1 30 6\n5 4 2 6 8 9\n5 20 10\n3 8 14 1 12 11 10 4 2 15\n###—###SEPERATOR—###—\n1\n0\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<iostream>
using namespace std;
void swap(int *xp,int *yp){
  int temp = *xp;
  	*xp=*yp;
  	*yp=temp;
}
int main()
{
  	int t,k,j;
  	cin>>t;
  	for(int i=0;i<t;i++){
    	int m,s,n,count=0;
      	cin>>m>>s>>n;
      	int a[n];
      	for(int k=0;k<n;k++){
        	cin>>a[k];
        }
      	for(j=0;j<n-1;j++){
        	for(k=0;k<n-j-1;k++){
            	if(a[k]>a[k+1]){
                	count++;
                }
            }
        }
      	if(m*60>s*count){
        	cout<<"1";
        }else{
        	cout<<"0";
        }
      	cout<<endl;
    }

	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