Sellers solution to a problem of dishonest practices c

Igor found a store is offering a discount, so I decided to buy n items in this store. After the shop discounts will last for a week, the price of each item during the discount is ai, and other discounts, prices of commodities becomes bi. But not all sellers are honest, so during the discount price of some commodities may be more expensive than the price after the end of discounts.

Igor decided to buy now at least k items, after the end of the rest of the purchase of goods and other activities. Your task is to help Igor calculate the lowest cost for the purchase of n items.

Input formats:

The first line contains two positive integers n and k (1≤n≤2e5,0≤k≤n), Igor represent the number of goods to be purchased and the number of items he now only buy less.

The second line contains n integers a1, a2, ..., an (1≤ai≤1e4), respectively, during discount prices of commodities.

The third line contains n integers b1, b2, ..., bn (1≤bi≤1e4), respectively, after the discount price of goods.

Output formats:

Igor minimum amount required to purchase goods n pieces.

3 1
1 3 5
6 4 2
 

Sample output:

6

 

 

Reading Comprehension : A store has n items on sale but due to some businesses compare black heart, when the price discount that n items is likely more expensive than no discount. Xiao Ming is now going to buy these n items, but not necessarily all be bought at the discount period. Before and after the price of goods are now given the number n and discounts, and quantity discounts Xiaoming during at least to be purchased. Find the lowest rates Xiaoming buy the n pieces of merchandise.

 

Idea : because the price of each item before and after the discount is fixed, so the interests of the Xiao Ming to buy items that can be obtained when the discount is fixed, that is, pre-discount price - after the discount price. Of course, since there are some black heart sellers, Xiao Ming benefits obtained could also be negative, we should try to avoid this situation. Therefore, we can calculate the data read in the interests of each item available Xiaoming, and stores it to the array Profile [], after which the array are arranged in descending further. N We want to sell items with a minimum price, so we should buy goods in the interests of greater than 0 discount as much as possible, buy interest in discounted merchandise is less than zero. However, because of the limited number of subject merchandise to us at the time of purchase discount should at least, this appears to be a bit several situations: 1. The limited number of goods purchased at least equal interest is greater than the number of goods less than zero, in which case the purchase n kinds of commodity costs in the cost required is the ideal case where: 0 is greater than or equal interest to buy goods at a discount, buy benefits count less than 0 at the end of the discount. 2. Limited number of goods purchased at least greater than the number of items of interest is greater than 0. Let k is the number of commodities to be purchased at least, at this time we only antecedent of interest k largest merchandise can be purchased at the discount. At this point, we can draw the following problem-solving process.

 

Process : Read Data -> front through each item discount price, obtaining each item of interest, and store in the array Profile [] in -> x n counting the number of items in the interests of greater than 0, and and determining the k -> the results obtained from the relationship of k and x.

Code :

#include <stdio.h>
int goods[200005][3];

void swap(int i,int j){
    int t1,t2,t3;
    t1=goods[i][0];t2=goods[i][1];t3=goods[i][2];
    goods[i][0]=goods[j][0];goods[i][1]=goods[j][1];goods[i][2]=goods[j][2];
    goods[j][0]=t1;goods[j][1]=t2;goods[j][2]=t3;
}

void qsort(int l,int r){
    int mid=goods[(l+r)/2][2],i=l,j=r;
    do{
        while(goods[i][2]>mid) i++;
        while(goods[j][2]<mid) j--;
        if(i<=j){
            swap(i,j);
            i++;j--;
        }
    }while(i<=j);
    if(i<r) qsort(i,r);
    if(j>l) qsort(l,j);
}

int main ()
{
    int n,k,i,sum=0;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&goods[i][0]);
    }
    for(i=0;i<n;i++){ //读入数据
        scanf("%d",&goods[i][1]);
        Goods [I] [ 2 ] = Goods [I] [ . 1 ] -goods [I] [ 0 ]; // calculate interest data read simultaneously 
    }
    qsort ( 0 , N- . 1 ); // sort 
    for (I = 0 ; I <k; I ++) { // top k items can be purchased directly before discount 
        SUM = Goods + [I] [ 0 ];
    }
    for (I = k; I <n-; I ++) { // the k items according to their interest situations (greater than 0 or less than 0) selected for later timing 
        IF (Goods [I] [ 2 ]> 0 ) SUM + = Goods [I ] [ 0 ];
         the else SUM = Goods + [I] [ . 1 ];
    }
    printf("%d",sum);
    return 0;
}

I like this teacher will not be playing (slide)

Guess you like

Origin www.cnblogs.com/fuck-you/p/12450521.html