Tuesday, 25 July 2017

Greedy Florist



A group of friends want to buy flowers where each flower has some base cost, . The florist wants to maximize his number of new customers, so he increases the price of flowers purchased by repeat customers; more precisely, if a customer has already purchased flowers, the price, , for flower is .
Given , , and the base cost for each flower, find and print the minimum cost for the group to purchase flowers.
Note: Flowers can be purchased in any order.
Input Format
The first line contains two space-separated integers describing the respective values of and .
The second line contains space-separated positive integers describing the respective values of .
Constraints

Output Format
Print the minimum cost to buy flowers.
Sample Input 0
3 3
2 5 6
Sample Output 0
13
Explanation 0
There are flowers with costs and people in the group. If each person buys one flower, the total cost of prices paid is dollars. Thus, we print as our answer.
Sample Input 1
3 2
2 5 6
Sample Output 1
15
Explanation 1
There are flowers with costs and people in the group. We can minimize the total purchase cost like so:
  1. The first person purchases flowers in order of decreasing price; this means they buy the more expensive flower () first at price dollars and the less expensive flower () second at price dollars.
  2. The second person buys the most expensive flower at price dollars.
We then print the sum of these purchases, which is , as our answer.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}


int getMinimumCost(int n, int k, int* c){
  
    int min_cost = 0;
    qsort(c,n,sizeof(int),cmpfunc);
    for (int i = n-1 ;i>=0 ;i--)
    {
     
        {
            min_cost = min_cost + (((n-i-1)/k)+1)*c[i];
        }
    }
  
    return min_cost ;
}

int main() {
    int n;
    int k;
    scanf("%d %d", &n, &k);
    int *c = malloc(sizeof(int) * n);
    for(int c_i = 0; c_i < n; c_i++){
       scanf("%d",&c[c_i]);
    }
    int minimumCost = getMinimumCost(n, k, c);
    printf("%d\n", minimumCost);
    return 0;
}

No comments:

Post a Comment