C语言 动态数组和快速排序实践

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/twentyonepilots/article/details/82731371

本题中注释部分是标准的快速排序,另外使用了动态数组。

Problem Description

Since milk packaging is such a low margin business, it is important to keep the price of the raw product (milk) as low as possible. Help Merry Milk Makers get the milk they need in the cheapest possible manner.

The Merry Milk Makers company has several farmers from which they may buy milk, and each one has a (potentially) different price at which they sell to the milk packing plant. Moreover, as a cow can only produce so much milk a day, the farmers only have so much milk to sell per day. Each day, Merry Milk Makers can purchase an integral amount of milk from each farmer, less than or equal to the farmer’s limit.

Given the Merry Milk Makers’ daily requirement of milk, along with the cost per gallon and amount of available milk for each farmer, calculate the minimum amount of money that it takes to fulfill the Merry Milk Makers’ requirements.

Note: The total milk produced per day by the farmers will be sufficient to meet the demands of the Merry Milk Makers.

Input

The first line contains two integers, N and M. The first value, N, (0 <= N <= 2,000,000) is the amount of milk that Merry Milk Makers’ want per day. The second, M, (0 <= M <= 5,000) is the number of farmers that they may buy from.

The next M lines (Line 2 through M+1) each contain two integers, Pi and Ai. Pi (0 <= Pi <= 1,000) is price in cents that farmer i charges. Ai (0 <= Ai <= 2,000,000) is the amount of milk that farmer i can sell to Merry Milk Makers per day.

Output

A single line with a single integer that is the minimum price that Merry Milk Makers can get their milk at for one day.

Sample Input

100 5 5 20 9 40 3 10 8 80 6 30

Sample Output

630

#include <stdio.h>
#include <stdlib.h>

void Input(int *p1,int *p2,int n){
    int i;
    for(i = 0 ; i<n;i++){
        scanf("%d %d",&p1[i],&p2[i]);
    }
}

/*
void sort(int *a, int left, int right)
{

    if(left >= right)
    {
        return ;
    }
    int i = left;
    int j = right;
    int key = a[left];

    while(i < j)
    {
        while(i < j && key <= a[j])
        {
            j--;
        }
        a[i] = a[j];
        while(i < j && key >= a[i])
        {
            i++;
        }

        a[j] = a[i];
    }

    a[i] = key;
    sort(a, left, j - 1);
    sort(a, i + 1, right);
}
*/

void sort(int *a, int *b,int left, int right)
{

    if(left >= right)
    {
        return ;
    }
    int i = left;
    int j = right;
    int key = a[left];
    int k=b[left];

    while(i < j)
    {
        while(i < j && key <= a[j])
        {
            j--;
        }
        a[i] = a[j];
        b[i]=b[j];
        while(i < j && key >= a[i])
        {
            i++;
        }

        a[j] = a[i];
        b[j]=b[i];
    }

    a[i] = key;
    b[i]=k;
    sort(a, b,left, j - 1);
    sort(a,b, i + 1, right);
}

int main(){
    int m,n;
    scanf("%d %d",&m,&n);
    int *p1=NULL  ;
    p1=(int *)malloc(n*sizeof(int));
    if(p1==NULL)exit(1);
    int *p2=NULL  ;
    p2=(int *)malloc(n*sizeof(int));
    if(p2==NULL)exit(1);
    Input(p1,p2,n);

    sort(p1,p2,0,n-1);


    int sum=0;
    int price=0;
    for(int i=0;i<n;i++){
        if(sum+p2[i]<=m){
        sum+=p2[i];
        price+=p1[i]*p2[i];
        }
        else{
            price+=p1[i]*(m-sum);
            break;
            }
        }
        printf("%d\n",price);

    free(p1);
    free(p2);

    return 0;
    }


猜你喜欢

转载自blog.csdn.net/twentyonepilots/article/details/82731371