Greedy Florist
Problem Statement :
A group of friends want to buy a bouquet of flowers. The florist wants to maximize his number of new customers and the money he makes. To do this, he decides he'll multiply the price of each flower by the number of that customer's previously purchased flowers plus 1. The first flower will be original price, ( 0 + 1 ) * original price , the next will be ( 1 + 1 ) * original price and so on. Given the size of the group of friends, the number of flowers they want to purchase and the original prices of the flowers, determine the minimum cost to purchase all of the flowers. Function Description Complete the getMinimumCost function in the editor below. It should return the minimum cost to purchase all of the flowers. getMinimumCost has the following parameter(s): c: an array of integers representing the original price of each flower k: an integer, the number of friends Input Format The first line contains two space-separated integers n and k, the number of flowers and the number of friends. The second line contains n space-separated positive integers c[ i ] , the original price of each flower. Constraints 1 <= n. k <= 100 1 <= c[ i ] <= 10^6 answer < 2^31 0 <= i < n Output Format Print the minimum cost to buy all n flowers.
Solution :
Solution in C :
In C :
#include <stdio.h>
#include <stdlib.h>
long long a[200],i,j,k,l,m,n;
int com(const void *xx, const void *yy)
{
if(*(long long *)xx > *(long long*)yy) return -1;
return 1;
}
int main()
{
scanf("%lld %lld\n",&n,&k);
for(i=0;i<n;i++) scanf("%lld",a+i);
qsort(a,n,sizeof(a[0]),com);
m = 0;
for(i=0;i<n;i++) m+= a[i]*(1+i/k);
printf("%lld\n",m);
return 0;
}
Solution in C++ :
In C++ :
#include <cstdio>
#include <algorithm>
using namespace std;
int a[102];
int main() {
int i,j,m,n;
long long c,ans=0;
scanf("%d%d",&n,&m);
for (i=0;i<n;++i) {
scanf("%d",&a[i]);
}
sort(a,a+n);
for (i=n-1,c=0,j=0;i>=0;--i) {
if (j==0) {
++c;
}
ans+=c*a[i];
if (++j==m) {
j=0;
}
}
printf("%Ld\n",ans);
return 0;
}
Solution in Java :
In Java :
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n, k;
n = in.nextInt();
k = in.nextInt();
int c[] = new int[n];
for (int i = 0; i < n; i++) {
c[i] = in.nextInt();
}
Arrays.sort(c);
int result = 0;
if(k >= n){
for(int i=0;i<n;i++){
result += c[i];
}
System.out.println(result);
}else{
//Processing
int x = 0;
while(n > 0){
for(int i=0;i<k;i++){
result += c[n-1]*(x+1);
n--;
if(n == 0){
break;
}
}
x++;
}
System.out.println(result);
}
}
}
Solution in Python :
In Python3 :
NK = input()
N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])
prices = [int(x) for x in input().strip().split(' ')]
if K == N:
print(sum(prices))
else:
prices.sort()
friends = [[] for friend in range(K)]
for friend in friends:
friend.append(prices.pop())
index = 0
while prices:
friends[index].append(prices.pop()*(1+len(friends[index])))
index = (index + 1)%K
total = 0
for friend in friends:
total += sum(friend)
print(total)
View More Similar Problems
Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →