SORT11
QUESTION DESCRIPTION
In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you.
You are now provided with an attractive offer.
You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free.
Now you have to answer two questions.
Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies.
Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies.
In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.
Mandatory conditions are "static void mergeSort(int a[],int l,int r)"
Input
The first line of the input contains T the number of test cases.
Each test case consists of two lines.
The first line of each test case contains the values of N and K as described above. Then in the next line N integers follow denoting the price of each of the N different candies.
Output
For each test case output a single line containing 2 space separated integers , the first denoting the minimum amount of money required to be spent and the second denoting the maximum amount of money to be spent.
Remember to output the answer of each test case in a new line.
Constraints
1 <= T <= 50
1 <= N <= 1000
0 <= K <= N-1
1 <= Ai <= 100
In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you.
You are now provided with an attractive offer.
You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free.
Now you have to answer two questions.
Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies.
Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies.
In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.
Mandatory conditions are "static void mergeSort(int a[],int l,int r)"
Input
The first line of the input contains T the number of test cases.
Each test case consists of two lines.
The first line of each test case contains the values of N and K as described above. Then in the next line N integers follow denoting the price of each of the N different candies.
Output
For each test case output a single line containing 2 space separated integers , the first denoting the minimum amount of money required to be spent and the second denoting the maximum amount of money to be spent.
Remember to output the answer of each test case in a new line.
Constraints
1 <= T <= 50
1 <= N <= 1000
0 <= K <= N-1
1 <= Ai <= 100
TEST CASE 2
INPUT
INPUT
2
6 2
5 4 2 6 1 8
9 4
2 7 1 6 8 3 12 10 11
OUTPUT3 14
3 23
#include<iostream>
using namespace std;
static void mergeSort(int a[],int l,int r)
{
int temp,numpurchase,min=0,max=0;
for(int i=0; i<l; i++)
{
for(int j=0; j<(l-i-1); j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
numpurchase=l/(r+1)+(l%(r+1)!=0);
for(int i=0; i<numpurchase; i++)
{
min+=a[i];
max+=a[l-i-1];
}
cout<<min<<" "<<max<<endl;
}
int main()
{
int t;
cin>>t;
for(int c=0; c<t; c++)
{
int l,r,a[20];
cin>>l>>r;
for(int i=0; i<l; i++)
cin>>a[i];
mergeSort(a, l, r);
}
return 0;
}
Comments
Post a Comment