首页 > 试题广场 >

小美的外卖节省钱计划

[编程题]小美的外卖节省钱计划
  • 热度指数:3039 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

2020年的618不再仅仅是购物节啦,同时也是美团外卖节,小美早早就准备好了各种满减代金券,为了最大程度的“省钱”,当然是选择把这些代金券都用光啦!

       这些代金券都有一个使用门槛,即满多少元的订单才可以使用。如果使用一个二元组<x,y>表示一张代金券,即需要满x元才能优惠y元,那么需要注意的是,并不是所有代金券的x都是大于等于y的,良心美团也会推出一些x<y的代金券。如果x<y,例如x=1y=2,则购买1元商品的情况下无需付款,不会退款给用户。

请问小美如果想用完这些代金券,在保证总付款金额最小的情况下,她最多购买多少钱的外卖呢?

说明:

1.一个订单只能用一张代金券。

2.同时满足总付款金额最少,且购买的外卖价值最高,例如两个优惠完都是1元的外卖,一个原价3元另一个原价4元,则选四元的。

3.由于美团商户很多,所以对于任何一个价格我们都可以找到至少一种商品购买。


输入描述:

输入第一行仅包含一个正整数n,表示小美拥有的代金券数量。(1<=n<=50000)

接下来有n行,每行有两个整数x和y,表示一张代金券需要订单金额满x元可以使用,能够优惠y元。(1<=x<=10000,1<=y<=10000)



输出描述:
输出仅包含两个正整数,中间用空格隔开,分别表示小美购买的外卖价值和她的实际付款金额。
示例1

输入

3
5 3
10 5
1 2

输出

17 7
题目要求把代金券用完,因此针对每张代金券都要下一个订单,分为以下两种情况:
(1) x >= y,这时候为了省钱且保证外卖价值更高,不能买价值超过x的外卖,否则自己就要贴更多的钱,所以花费x-y元获得价值为x的外卖。
(2) x < y,此时可以免费获得价值为x的外卖,但是为了购买的外卖价值更高,选择价值能够达到y的外卖,这样就能够免费获得价值为y(因为x < y,仍然满足代金券的使条件)的外卖,收益更大。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().trim());
        int value = 0, cost = 0;
        int x, y;
        for(int i = 0; i < n; i++){
            String[] pair = br.readLine().trim().split(" ");
            x = Integer.parseInt(pair[0]);
            y = Integer.parseInt(pair[1]);
            if(x >= y){
                value += x;
                cost += x - y;
            }else
                value += y;
        }
        System.out.println(String.format("%d %d", value, cost));
    }
}


编辑于 2021-02-28 17:20:12 回复(0)
void async function () {
    let num=await readline();
    let yh=[];
    for(let i=0;i<Number(num);i++){
        let line=await readline()
        yh.push(line.split(' '))
    }
    let total=0;let pay=0;
    yh.map(item=>{
        item=item.map(Number);
        if(item[0]<item[1]){
            total+=item[1];
        }else{
            total+=item[0];
            pay+=(item[0]-item[1])
        }
    })
    console.log(total+' '+pay)
}()
编辑于 2023-03-16 16:38:59 回复(0)

# 提取第x人的分数 score[x-1]

# 大于等于第x人的分数为晋级人数 score[i]

a = list(map(int,input().strip().split()))

n,x = a

score = list(map(int,input().strip().split()))

score.sort()

 

num = 0

for i in range(n):

    if score[i]>=score[n-x-1] and score[i]!=0:

        num += 1

print(num)
发表于 2023-08-02 10:52:55 回复(0)
这题没什么意思,最大的难点就是读懂题目
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int[] x = new int[n];
        int[] y = new int[n];

        for(int i = 0; i < n; i++)
        {
            x[i] = in.nextInt();
            y[i] = in.nextInt();
        }
        int value = 0;
        int cost = 0;
        for(int i = 0; i < n; i++)
        {
            if(x[i] > y[i])
            {
                value += x[i];
                cost += x[i] - y[i];
            } else {
                value += y[i];
            }
        }
        System.out.printf("%d %d", value, cost);
    }
}


发表于 2023-04-20 00:43:37 回复(0)
#include<bits/stdc++.h>
usingnamespacestd;
 
intmain()
{
    intn;cin>>n;
    vector<vector<int>>juan (n,vector<int>(2,0));
    intvalue=0;intmoney=0;
    for(inti=0;i<n;i++)
    {
        cin>>juan[i][0]>>juan[i][1];
        if(juan[i][0]<juan[i][1]) value+=juan[i][1];
        else
        {
            value+=juan[i][0];
            money+=(juan[i][0]-juan[i][1]);
        }
    }
    cout<<value<<" "<<money;
}
发表于 2022-08-06 09:20:47 回复(0)
#include<iostream>
using namespace std;


int worth_money(int arr[][2], int len)
{
    int sum_money = 0;

    for (int i = 0; i < len; i++)
    {
        if (arr[i][0] < arr[i][1])
        {
            sum_money += (arr[i][1] / arr[i][0]) * arr[i][0];
        }
        else
        {
            sum_money += arr[i][0];
        }
    }
    return sum_money;
}
int pay_money(int arr[][2], int len)
{
    int sum_pay = 0;

    for (int i = 0; i < len; i++)
    {
        if (arr[i][0] < arr[i][1])
        {
            sum_pay += 0;
        }
        else
        {
            sum_pay += (arr[i][0] - arr[i][1]);
        }
    }
    return sum_pay;
}


int main()
{
    int n;
    cout << "请输入n:" << endl;
    cin >> n;
    int arr[50000][2];
    for (int i = 0; i < n; i++)
    {
        cout << "请输入x= " << endl;
        cin >> arr[i][0];
        cout << "请输入y= " << endl;
        cin >> arr[i][1];
    }

    cout << worth_money(arr,n) << "\t" << pay_money(arr,n);


}
发表于 2022-08-04 10:58:09 回复(0)
JavaScript 代码,做了这题之后才知道满减原来是这样凑的,凑满10元减5元,再凑5元减3元,总价值15元,实付7元这样。
const n = parseInt(readline())
let value = 0
let pay = 0
for(let i = 0; i < n; i++) {
    const [x, y] = readline().split(' ').map(Number)
    if(x <= y) {
        value += y
    } else {
        value += x
        pay += (x - y)
    }
}
console.log(value + ' ' + pay)

发表于 2022-05-22 19:53:23 回复(0)
题目要求实付金额最小的前提下外卖价值(即原价)最高,所以
对于x>y的代金券,一定会购买价值x元的外卖,实付金额为x-y;
对于x<=y的代金券,可以选择购买价值为x到y的外卖,实付金额都为0,而为了使外卖价值最高,我们可以认为这类代金券的x=y;
按照以上过程优化之后,直接对所有代金券的x求和可得到外卖总价值,对所有x-y求和可得到实付金额

n=int(input()) 
lst=[[int(j) for j in input().split()] for i in range(n)]
value=0 # value代表总价值
money=0 # money代表最终的实付金额
for i in range(n):
    if lst[i][0]<lst[i][1]:
        lst[i][0]=lst[i][1]
    value+=lst[i][0]
    money+=lst[i][0]-lst[i][1]
print(value,money)

发表于 2022-05-21 11:07:08 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] map = new int[n][2];
        int sum = 0;
        int cost = 0;
        for (int i = 0; i < n; i++) {
            map[i][0] = sc.nextInt();//满多少元
            map[i][1] = sc.nextInt();//减多少元
        }
        for (int i = 0; i < n; i++) {
            sum += Math.max(map[i][0],map[i][1]);//满一定减,减大于满的取减
            cost += map[i][1];//所有减
        }
        System.out.println(sum+" "+(sum-cost));
    }
}

发表于 2022-04-09 01:49:57 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int value = 0,ac = 0;
        int a,b;
        while(num > 0){
            a = sc.nextInt();
            b = sc.nextInt();
            if(a > b){
                value += a;
                ac += a - b;
            }else{
                value += b;
            }
            num--;
        }
        System.out.println(value + " " + ac);
    }
}

发表于 2022-03-22 21:58:00 回复(0)
#include<bits/stdc++.h>
using namespace std;
vector<int> sovle(vector<vector<int>>&vec)
{
    vector<int>res(2,0);
    if(vec.size()==0)return res;
    int totalValue=0;
    int realPay=0;
    sort(vec.begin(),vec.end(),[&](vector<int> &a,vector<int>&b){return a[0]<b[0];});
    for(auto &l:vec)
    {
        if(l[0]>l[1])
        {
            totalValue+=l[0];
            realPay+=(l[0]-l[1]);
        }else{
            totalValue+=l[1];
        }
    }
    res[0]=totalValue;
    res[1]=realPay;
    return res;
}
int main()
{
    int n;
    cin>>n;
    vector<vector<int>>vec;
    vector<int>temp(2,0);
    while(n--)
    {
        int a,b;
        cin>>a>>b;
        temp[0]=a;
        temp[1]=b;
        vec.push_back(temp);
    }
    vector<int>res=sovle(vec);
    cout<<res[0]<<" "<<res[1]<<endl;
    return 0;
 
}
发表于 2021-09-15 20:11:04 回复(0)
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int>  PII;

int n;
vector<PII> num;

int main()
{
    cin >> n;

    for(int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        num.push_back({a, b});
    }

    int value = 0, pay_value = 0;
    for(int i = 0; i < n; i++)
    {
        int a = num[i].first;
        int b = num[i].second;

        if(a >= b)
        {
            value += a;
            pay_value += (a - b);
        }
        else 
            value += b;
    }
    cout << value << " " << pay_value << endl;

    return 0;
}

发表于 2021-08-20 16:42:45 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = 0;
        int real = 0;
        for(int i=0;i<n;i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(a>=b){
                max = max+a;
                real = real+a-b;
            }else{
                max = max+b;
            }
        }
        System.out.println(max+" "+real);
    }
}
发表于 2021-08-13 20:50:40 回复(0)

import java.util.*;



public class Main 
{    
    
    public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] x = new int[n];
            int[] y = new int[n];
            int value = 0;
            int cost = 0;
            for (int i = 0; i < n; i++) 
            {
                x[i] = sc.nextInt();
                y[i] = sc.nextInt();
                if(x[i] > y[i])
                {
                    value += x[i];
                    cost += x[i] - y[i];
                }
                else
                {
                    value += y[i];
                }
            }
            System.out.println(value + " " + cost);
            
        }

}

发表于 2021-05-09 00:10:30 回复(0)
n = int(input())
buy = pay = 0
while True:
    try:
        x, y = map(int, input().split())
        if x > y:
            buy += x
            pay += x - y
        else:
            buy += y
    except:
        break
print('{} {}'.format(buy, pay))

发表于 2021-03-12 17:08:43 回复(0)
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    int n;
    cin >> n;
    int totalWorth = 0, totalCount = 0;
    while (n--) {
        int w, c;
        cin >> w >> c;
        if (w <= c) {
            totalWorth += c;
        } else {
            totalWorth += w;
            totalCount += (w - c);
        }
    }
    cout << totalWorth << ' ' << totalCount << endl;
     
    return 0;
}

发表于 2021-03-02 11:38:55 回复(0)
n = int(input())
values = 0
give_money = 0
for i in range(n):
    x,y = map(int, input().split())
    if x>=y:
        give_money+=(x-y)
        values+=x
    else:
        give_money+=0
        values+=y   
         
print(values,give_money)

发表于 2021-03-01 19:50:48 回复(0)