浙江大学软件学院 2020 年保研机考 7-4 Shopping With Coupons (C++)

There are N N N items on your shopping list, and you have N N N kinds of coupon that can save you some money on buying any of these items. At the mean time, you have D D D dollars in your pocket. How can you buy as many items as you can? Here you may use any coupon many times and buy any item many times, but only one coupon may be used to get a reduction of payment for one item once – that is, for example, you may use coupon A1 to buy item B1, and then use coupon A2 to buy item B1. At the mean time, coupon A1 can be used to buy item B2. But you may not use coupon A1 to buy item B1 AGAIN.

For example, suppose there are 4 items with prices of 10 10 10, 12 12 12, 15 15 15 and 20 20 20; and 4 kinds of coupons with values of 6 6 6, 7 7 7, 8 8 8 and 9 9 9. If you have $30 in your pocket, the best way to buy is the following:

  • buy 10 item for 4 times,with each coupon once, and hence pay 10 x 4 - 6 - 7 - 8 - 9 = 10 in total;
  • buy 12 item for 3 times, with coupons of 7, 8 and 9, and hence pay 12 x 3 - 7 - 8 - 9 = 12 in total;
  • buy 15 item with 9 coupon, and hence pay 6;

Now you have $2 left, not enough to buy any more items. Therefore the maximum number of items you can buy is 8.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: N ( ≤ 1 0 5 ) N (≤10^5) N(105), the number of items (and the coupons), and D ( ≤ 1 0 6 ) D (≤10^6) D(106), the amount of money you have.

Then N N N positive prices are given in the second line, and N N N positive coupon values are given in the third line. It is guaranteed that the highest value of coupons is less than the lowest price of items, and all the numbers are bounded above by 1 0 9 10^9 109. The numbers in a line are separated by spaces.

Output Specification:

Print in a line the maximum number of items you can buy, and the maximum amount of money left, separated by 1 space.

Sample Input:

4 30
12 20 15 10
9 6 8 7

Sample Output:

8 2

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-09-19 14:45:31
// All rights reserved.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Node{
    int itemsID;
    int couponsID;
    int diff;

    Node(){
        itemsID = 0;
        couponsID = 0;
        diff = 0;
    }
    ~Node(){}
}tree[100005];

int n, d;
int items[100005];
int coupons[100005];

void guolvDown(int a){
    int tmp = a;
    while(tmp * 2 <= n){
        if(tmp * 2 == n){
            if(tree[tmp].diff > tree[n].diff){
                swap(tree[tmp], tree[n]);
                tmp = n;
            }

            break;
        }
        else{
            if(tree[tmp].diff <= tree[tmp * 2].diff && tree[tmp].diff <= tree[tmp * 2 + 1].diff) break;

            if(tree[tmp * 2].diff <= tree[tmp].diff && tree[tmp * 2].diff <= tree[tmp * 2 + 1].diff){
                swap(tree[tmp], tree[tmp * 2]);
                tmp = tmp * 2;
            }
            else{
                swap(tree[tmp], tree[tmp * 2 + 1]);
                tmp = tmp * 2 + 1;
            }
        }
    }
}

int main(){
    scanf("%d %d", &n, &d);

    for(int i = 0; i < n; ++i) scanf("%d", &items[i]);
    for(int i = 0; i < n; ++i) scanf("%d", &coupons[i]);
    sort(items, items + n);
    sort(coupons, coupons + n, greater<int>());
    coupons[n] = -d;
    
    int cnt = 0;
    int money = d;

    for(int i = 1; i <= n; ++i){
        tree[i].itemsID = i - 1;
        tree[i].couponsID = 0;
        tree[i].diff = items[i - 1] - coupons[0];
    }

    for(int i = n / 2; i >= 1; --i){
        if(2 * i + 1 > n){
            if(tree[i].diff > tree[2 * i].diff) swap(tree[i], tree[2 * i]);
        }
        else{
            if(tree[i].diff <= tree[2 * i].diff && tree[i].diff <= tree[2 * i + 1].diff) continue;
            if(tree[2 * i].diff <= tree[i].diff && tree[2 * i].diff <= tree[2 * i + 1].diff) swap(tree[i], tree[2 * i]);
            else swap(tree[i], tree[2 * i + 1]);
        }
    }

    while(money >= tree[1].diff){
        cnt++;
        money -= tree[1].diff;
        
        tree[1].couponsID++;
        tree[1].diff = items[tree[1].itemsID] - coupons[tree[1].couponsID];

        guolvDown(1);
    }

    printf("%d %d\n", cnt, money);

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值