usaco3.3.2 Shopping Offers

【USACO题库】3.3.2 Shopping Offers商店购物 题目描述 在商店中,每一种商品都有一个价格(用整数表示)。例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z 。为了吸引更多的顾客,商店举行了促销活动。 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是 12z。 编写一个程序,计算顾客购买一定商品的花费,尽量利用优惠使花费最 阅读详情

一 原题

Shopping Offers
IOI'95

In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more customers, the shop introduces some special offers.

A special offer consists of one or more product items together for a reduced price, also an integer. Examples:

  • three flowers for 5z instead of 6z, or
  • two vases together with one flower for 10z instead of 12z.

Write a program that calculates the price a customer has to pay for a purchase, making optimal use of the special offers to make the price as low as possible. You are not allowed to add items, even if that would lower the price.

For the prices and offers given above, the (lowest) price for three flowers and two vases is 14z: two vases and one flower for the reduced price of 10z and two flowers for the regular price of 4z.

PROGRAM NAME: shopping

INPUT FORMAT

The input file has a set of offers followed by a purchase.

Line 1:s, the number of special offers, (0 <= s <= 99).
Line 2..s+1:Each line describes an offer using several integers. The first integer is n (1 <= n <= 5), the number of products that are offered. The subsequent n pairs of integers c and k indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are part of the offer. The last number p on the line stands for the reduced price (1 <= p <= 9999). The reduced price of an offer is less than the sum of the regular prices.
Line s+2:The first line contains the number b (0 <= b <= 5) of different kinds of products to be purchased.
Line s+3..s+b+2:Each of the subsequent b lines contains three values: c, k, and p. The value c is the (unique) product code (1 <= c <= 999). The value k indicates how many items of this product are to be purchased (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). At most 5*5=25 items can be in the basket.

SAMPLE INPUT (file shopping.in)

2
1 7 3 5
2 7 1 8 2 10
2
7 3 2
8 2 5

OUTPUT FORMAT

A single line with one integer: the lowest possible price to be paid for the purchases.

SAMPLE OUTPUT (file shopping.out)

14


二 分析

DP。最多可以买5种商品,用dp[x1][x2][x3][x4][x5]代表5种物品分别买xi件的最低价格。少写一个头文件CE了一次。


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: shopping
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4220 KB]
   Test 2: TEST OK [0.000 secs, 4220 KB]
   Test 3: TEST OK [0.000 secs, 4220 KB]
   Test 4: TEST OK [0.000 secs, 4220 KB]
   Test 5: TEST OK [0.011 secs, 4220 KB]
   Test 6: TEST OK [0.000 secs, 4220 KB]
   Test 7: TEST OK [0.000 secs, 4220 KB]
   Test 8: TEST OK [0.000 secs, 4220 KB]
   Test 9: TEST OK [0.000 secs, 4220 KB]
   Test 10: TEST OK [0.054 secs, 4220 KB]
   Test 11: TEST OK [0.097 secs, 4220 KB]
   Test 12: TEST OK [0.076 secs, 4220 KB]

All tests OK.

Your program ('shopping') produced all correct answers! This is your submission #2 for this problem. Congratulations!


AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:shopping
*/

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAX_ID = 1000;
const int MAX_ITEM = 5;
const int MAX_NUM = 6;
const int MAX_PRICE = 1000;

int S, N, C, K, P, B;
int hashId[MAX_ID];
int target[MAX_ITEM];
int dp[MAX_NUM][MAX_NUM][MAX_NUM][MAX_NUM][MAX_NUM];

struct Item {
    int id;
    int num;
    
    int getHashId() {
        return hashId[id];
    }
};

Item item(const int &_id, const int &_num) {
    Item ret;
    ret.id = _id;
    ret.num = _num;
    return ret;
}

struct Discount {
    vector<Item> items;
    int price;
    
    bool can(int i, int j, int k, int l, int m) {
        for(int _i = 0; _i < items.size(); _i++) {
            Item item = items[_i];
            int id = item.getHashId();
            if(id == -1) return false;
            if(id == 0 && item.num > i) return false;
            if(id == 1 && item.num > j) return false;
            if(id == 2 && item.num > k) return false;
            if(id == 3 && item.num > l) return false;
            if(id == 4 && item.num > m) return false;
        }
        return true;
    }

    int get(int idx) {
        for(int i = 0; i < items.size(); i++) {
            if(items[i].getHashId() == idx) 
                return items[i].num;
        }
        return 0;
    }
};
vector<Discount> discounts;

Discount discount(const vector<Item> &_items, const int &_price) {
    Discount ret;
    ret.items = _items;
    ret.price = _price;
    return ret;
}

void init() {
    memset(hashId, -1, sizeof(hashId));

    scanf("%d", &S);
    for(int i = 0; i < S; i++) {
        scanf("%d", &N);
        vector<Item> items;
        for(int j = 0; j < N; j++) {
            scanf("%d%d", &C, &K);
            items.push_back(item(C, K));
        }
        scanf("%d", &P);
        discounts.push_back(discount(items, P));
    }

    scanf("%d", &B);
    int cnt = 0;
    for(int i = 0; i < B; i++) {
        scanf("%d%d%d", &C, &K, &P);
        vector<Item> items;
        items.push_back(item(C, 1));
        discounts.push_back(discount(items, P));
        hashId[C] = cnt;
        target[cnt] = K;
        cnt++;
    }
    
    for(int i = 0; i <= target[0]; i++)
    for(int j = 0; j <= target[1]; j++)
    for(int k = 0; k <= target[2]; k++)
    for(int l = 0; l <= target[3]; l++)
    for(int m = 0; m <= target[4]; m++)
        dp[i][j][k][l][m] = MAX_PRICE * MAX_ITEM * MAX_NUM;
}

void solve() {
    dp[0][0][0][0][0] = 0;
    for(int i = 0; i <= target[0]; i++)
    for(int j = 0; j <= target[1]; j++)
    for(int k = 0; k <= target[2]; k++)
    for(int l = 0; l <= target[3]; l++)
    for(int m = 0; m <= target[4]; m++) {
        for(int n = 0; n < discounts.size(); n++) {
            Discount d = discounts[n];
            if(d.can(i, j, k, l, m)) {
                dp[i][j][k][l][m] = min(dp[i][j][k][l][m],
                        dp[i-d.get(0)][j-d.get(1)][k-d.get(2)][l-d.get(3)][m-d.get(4)] + d.price);
            }
        }
    }
    printf("%d\n", dp[target[0]][target[1]][target[2]][target[3]][target[4]]);
}

int main() {
    freopen("shopping.in", "r", stdin);
    freopen("shopping.out", "w", stdout);
    init();
    solve();
    return 0;
}



P2732 [USACO3.3]商店购物 Shopping Offers 题目传送门:P2732 [USACO3.3]商店购物 Shopping Offers 这道题乍一看,压根看不懂,再看,看懂题了,第三眼,发现是动规,冥思苦想了半天想出来一个特别暴力的办法 首先我瞄准了小数据,最多只会有 5 种,所以我决定直接五维 dp f[i][j][k][l][m]f[i][j][k][l][m]f[i][j][k][l][m] 表示在第一个物品有 iii 个,表示在第二个物品有 jjj 个,表示在第三个物品有 kkk 个,表示在第四个物品有 lll 个,表示在第五个物品有 mmm 个, 阅读详情

相关推荐

利用Ghidra与SVD插件实现STM32固件深度逆向分析

本文详细介绍了如何利用Ghidra逆向工程框架结合SVD插件,实现对STM32固件(.bin文件)的深度逆向分析。通过加载描述芯片外设寄存器的SVD文件,Ghidra能将晦涩的硬件地址操作转换为可读的寄存器符号,极大提升了对嵌入式固件中通信协议、安全校验等关键逻辑的分析效率。文章涵盖了从Java环境搭建、工具配置到实战逆向解析的完整流程。

peach的博客 942

[USACO3.3] 商店购物 Shopping Offers

在商店中,每一种商品都有一个价格(用整数表示)。例如,一朵花的价格是 $2$ ,而一个花瓶的价格是 $5$。为了吸引更多的顾客,商店举行了促销活动。

ahui_is_so_cool的博客 324

AD21导出gerber文件

文件–>制造输出–>Gerber Files 文件–>制造输出–>Gerber Files 通用、光圈和高级跟第一步一样 3.文件–>制造输出–>NC Drill Files 这个默认就可以了 主要参考视频来自bilibili添加链接描述 此篇博客主要是写给自己好记的,无其他商业用途 ...

新手小杨 1万+

USACO3.3--Shopping Offers

如果我们将每一种优惠方案看成一种物品,那么这个问题就可以转换成背包问题,我们可以定义dp[a1][a2][a3][a4][a5]表示购买第一到第五中物品ai个时的最小代价,然后转移方程类似于完全背包dp[a1][a2][a3][a4][a5]=min(dp[a1-st[i].num[a1]][a2-st[i].num[a2]…[a5-st[i].num[a5]);但是这样还不够,因为根据题目要求其实

荒野孤鹰 690

汽车以太网- IEEE 802.3标准

关于汽车以太网,大家看到最多的标准大概就是IEEE802.3bw,也就是我们车内使用的百兆以太网,专业称呼是100BASE-T1,该标准是车内百兆以太网的物理层协议,是我们学习汽车以太网必须要了解的协议。在正式了解协议之前,我们先介绍下什么是IEEE802.3标准。............

weixin_39974126的博客 7107

基于IMU的纯惯性导航轨迹绘制实践与优化

本文深入探讨了基于IMU的纯惯性导航轨迹绘制实践。通过修改开源组合导航算法KF-GINS,屏蔽GPS更新并简化地球参数,实现了仅依赖IMU数据的轨迹解算。文章详细介绍了代码改造、轨迹可视化方法,并分析了误差累积的根本原因,为深入理解惯性导航原理与传感器融合的必要性提供了宝贵的实践经验。

ff678的博客 395

USACO 3.3 Shopping Offers

Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more c...

weixin_30438813的博客 180

gmsv源代码c语言,石器时代ABLua的原理、简介、过程、运用、收发

操作原理(一)服务端:服务器源文件客户端:客户所操作的程序(既是游戏客户端)接口:既是某项功能(例如:鼠标点击,文字发送,战斗,观战,人物动作等)返回的值ABlua:在源代码中添加的整套引擎作为回调接口--lua所实现的功能是基于服务端源码添加的接口由该接口与客户端发送封包实现功能源码里是否有ABlua回调接口?例如:源码里的gmsv/battle.c文件(既是战斗回调文件)以下为简单描述C给予l...

weixin_35223575的博客 2408

USACO-Section3.3 Shopping Offers【完全背包】

题目描述: 在商店中,每一种商品都有一个价格(用整数表示)。例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z 。为了吸引更多的顾客,商店举行了促销活动。 促销活动把一个或多个商品组合起来降价销售,例如: 三朵花的价格是 5z 而不是 6z, 两个花瓶和一朵花的价格是 10z 而不是 12z。 编写一个程序,计算顾客购买一定商品的花费,尽量利用优惠使花费最少。尽管有...

tjj1998的博客 694

LoRaWAN数据包分析工具

前言:LoRaWAN的射频数据包是加密的,而且有些字段是某个字节的某几位,看着不是那么直观,因此在某些情况下需要借助工具来分析数据包。 1、安装npm 这个工具是用JavaScript写的,其中用到了一些依赖库,而npm就可以一次性把这些依赖库下载下来,因此我们先要下载安装npm这个工具。 安装:sudo apt install npm 输入y,等待安装完成: 检查是否安装成功:npm -v 说明安装完成。 2、安装LoRa数据包分析工具lora-packet 安装:npm i.

freemote的博客 4629

Python量化交易实战:从零搭建你的第一个多因子选股模型(附完整代码)

本文是一份详细的Python量化交易入门实战指南,手把手教你从零搭建第一个多因子选股模型。内容涵盖环境配置、数据获取、因子挖掘与有效性检验、多因子合成以及完整的策略回测与绩效评估,并附有可直接运行的完整代码,帮助初学者快速掌握量化交易的基本概念与实践流程。

resnet7explorer的博客 934

USACO 3.3.2 Shopping Offers解题报告

写在前面:因为之前没写的C++的USACO Training的解题报告太多……所以就不写了,要是想要代码可以联系我:xiedong_1993@foxmail.com 这题就是传说中的五维背包,其实写起来难度不大。但是我写的时候可以说是遇到了重重困难。因为我对C++的掌握还不如Pascal,所以很多东西写的不是很熟。这题有一个很大的问题就在于,最后有用的只有五种物品,但是物品的编号却又很...

diekang6987的博客 272

uCOS中的消息邮箱(:task与task之间传递一个数据(数据缓冲区))

程序需求,需要完成task与task之间  字符数组的数据传输 比如:在ucos中,串口2接收的字符数组是不定时的&不定长的;只有当接收到数据的时候_才将数据赋值给新数组 具体的程序示意如下: 图1:消息邮箱测试中的     向消息邮箱发送消息(OSMboxPost(Str_Box,(void*)rec);//发送消息--(向 消息邮箱_msg_key发送消息) ),实际项目需求的

faguoduifangui的博客 4540

[USACO3.3.2]Shopping Offers

Shopping Offers IOI'95 In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more cust

qq_37665716的博客 299

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

USACO3.3.2 Shopping Offers (shopping)

因为n 然后就记忆搜吧,没什么技术含量。/* ID:shijiey1 PROG:shopping LANG:C++ */ #include #include #include #define INF 0x3f3f3f3f using namespace std; int s, b, m; // shop[i][0]表示第i种优惠的价格,shop[i][1],shop[i][2],sh

Shion的专栏 543

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

C++&Pascal——【USACO 3.3.2】——Shopping Offers

Shopping Offers IOI'95 In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more cust

McDonnell_Douglas的博客 775

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Shopping Offers (shopping)[USACO 3.3.2]

算法:DP   分析:一个经典的多重背包问题。每一个物品按原价出售也可以看成是一种优惠方案,因此一共有N+M种优惠方案,然后用这N+M种优惠方案组成需要的物品数量的背包。       五维DP,这道题想倒是不难想,就是在对于这种关系的处理上不太容易理请思路。 progr

262

多维完全背包 USACO 3.3.2 Shopping Offers

典型多维完全背包 /* PROG:fence LANG:C++ */ #include #include using namespace std; const int MAXN = 501; int d[MAXN], tot, ans[10000], n, up, down = ~0U>>1; int a[MAXN][MAXN]; void dfs(int x) {

IcEternal 609

USACO 3.3 Shopping Offers (shopping)

/* Main idear DP problem. Take each offer as a single choise for DP. dp[a1][a2][a3][a4][a5] = min{dp[a1-prodcut_num[i][1]][a2-prodcut_num[i][2]] [a1-prodcut_num[i][3]][a1-prodc

Hello World! 1527

Shopping Offers问题及解法

638. Shopping Offers LeetCode

刘天的博客 624
上一篇: cf 754D Fedor and coupons
下一篇: usaco3.3.3 Camelot
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值