USACO 3.3 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 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……
但是我的暴搜没舍得扔,能过7个点
正解:
/*
ID: ivorysi
PROG: shopping
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int id[],cnt;
int s;
int sale[][][][][],item[][][][][];
int ti[];
int reg[];
void init() {
scanf("%d",&s);
int n,p,k[],c,b,t;
memset(sale,inf,sizeof(sale));
siji(i,,s) {
scanf("%d",&n);
memset(k,,sizeof(k));
siji(j,,n) {
scanf("%d%d",&c,&t);
if(id[c]==) id[c]=++cnt;
k[id[c]]=t;
}
scanf("%d",&p);
sale[k[]][k[]][k[]][k[]][k[]]=min(p,sale[k[]][k[]][k[]][k[]][k[]]);
}
scanf("%d",&b);
siji(i,,b) {
scanf("%d%d%d",&c,&t,&p);
if(id[c]==) id[c]=++cnt;
ti[id[c]]=t;
reg[id[c]]=p;
}
siji(i,,) {
siji(j,,){
siji(m,,) {
siji(o,,) {
siji(l,,) {
item[i][j][m][o][l]=i*reg[]+j*reg[]+m*reg[]+o*reg[]+l*reg[];
}
}
}
}
}
}
int a1,a2,a3,a4,a5;
void calc(int &x) {
siji(i1,,a1) {
siji(i2,,a2) {
siji(i3,,a3){
siji(i4,,a4){
siji(i5,,a5) {
int temp=sale[i1][i2][i3][i4][i5]+item[a1-i1][a2-i2][a3-i3][a4-i4][a5-i5];
x=min(x,temp);
}
}
}
}
}
}
void solve() {
init();
for(a1=;a1<=ti[];++a1) {//a1,a2等都要重新赋为0
for(a2=;a2<=ti[];++a2) {
for(a3=;a3<=ti[];++a3) {
for(a4=;a4<=ti[];++a4){
for(a5=;a5<=ti[];++a5) {
calc(item[a1][a2][a3][a4][a5]);
}
}
}
}
}
printf("%d\n",item[ti[]][ti[]][ti[]][ti[]][ti[]]);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("shopping.in","r",stdin);
freopen("shopping.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
暴搜:
/*
ID: ivorysi
PROG: shopping
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int s;
struct state {
int val,item[];
int& operator[](int x) {return item[x];}
bool operator < (const state& rhs)const {
bool flag=;
siji(i,,) {
if(item[i]>rhs.item[i]) {flag=;break;}
}
return flag;
}
bool operator ==(const state& rhs)const{
return !memcmp(rhs.item,item,sizeof(item));
}
bool operator <=(const state& rhs)const{
return (*this<rhs)||(*this==rhs);
}
state operator -(const state& rhs)const {
state res;
siji(i,,) {
res[i]=item[i]-rhs.item[i];
}
return res;
}
}qwq,sale[];
int re[];
int ans;
void init() {
scanf("%d",&s);
int n,p,k,c,b;
siji(i,,s) {
scanf("%d",&n);
siji(j,,n) {
scanf("%d%d",&c,&k);
sale[i][c]=k;
}
scanf("%d",&p);
sale[i].val=p;
}
scanf("%d",&b);
siji(i,,b) {
scanf("%d%d%d",&c,&k,&p);
qwq[c]=k;qwq.val+=k*p;
re[c]=p;
}
siji(i,,s) {
int z=;
siji(j,,) {
z+=sale[i][j]*re[j];
}
sale[i].val-=z;
}
ans=qwq.val;
}
void dfs(state x) {
if(x.val<ans) ans=x.val; siji(i,,s) {
state nw=x;
while(sale[i]<=x) {
nw=nw-sale[i];
nw.val+=sale[i].val;
dfs(nw);
}
}
}
void solve() {
init();
dfs(qwq);
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("shopping.in","r",stdin);
freopen("shopping.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
USACO 3.3 Shopping Offers的更多相关文章
- 洛谷P2732 商店购物 Shopping Offers
P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...
- poj 1170 Shopping Offers
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4696 Accepted: 1967 D ...
- LeetCode 638 Shopping Offers
题目链接: LeetCode 638 Shopping Offers 题解 dynamic programing 需要用到进制转换来表示状态,或者可以直接用一个vector来保存状态. 代码 1.未优 ...
- HDU 1170 Shopping Offers 离散+状态压缩+完全背包
题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...
- 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)
作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...
- Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)
Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...
- LC 638. Shopping Offers
In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...
- Week 9 - 638.Shopping Offers - Medium
638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...
- POJ 1170 Shopping Offers非状态压缩做法
Shopping Offers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5659 Accepted: 2361 Descr ...
随机推荐
- 企业架构研究总结(44)——企业架构与建模之Archimate视图和视角
3. ArchiMate的视角与视图 创建.维护一个企业架构是一件非常复杂繁琐的事情,因为这项工作需要面对许多背景.利益各异的干系人,对他们所关注的问题进行解答,并能够在他们之间形成无障碍的沟通流.为 ...
- ie8下下拉菜单文字为空
<html> <head> <title></title> <script type="text/javascript"> ...
- PHP中的赋值-引用or传值?
直接上代码: <?php $num1 = 1; $num2 = $num1; $num1 = 2; echo $num2 . "\n"; $arr1 = array(1, 2 ...
- 免费UI框架推荐--Charisma UI
基于Jquery.Bootstrap的后台管理免费UI框架推荐--Charisma UI 在项目设计和开发工作中,做过一些后台管理系统的设计和开发,用的很多都是比较传统的UI框架. 老是走在这个圈子里 ...
- SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)
挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 这个工具主要就是自动备份数据库,一键还原数据库,发送备份数据库日志报告到邮箱,自动压缩备份好的数据库 定期执行数 ...
- 搭建一个完整的Java开发环境
搭建一个完整的Java开发环境 作为一个Java程序员,配置一个java开发环境是必备的技能,今天给广大菜鸟初学者补上一课.环境的配置,大概就分三个1,JDK 2,Tomcat(或者其他的)3,ecl ...
- Redis系统学习 四、超越数据结构
5种数据结构组成了Redis的基础,其他没有关联特定数据结构的命令也有很多.我们已经看过一些这样的命令:info,select,flushdb,multi,exec,discard,watch,和ke ...
- mvc使用JsonResult返回Json数据
mvc使用JsonResult返回Json数据 controller 中定义以下方法: public JsonResult UpdateSingle(int id, string actionNa ...
- node.js系列笔记之fs模块《二》
一:感触 最近工作比较忙,感觉也比较多,因为工作上的不顺利,再加上加班比较多,所以最近心情不是很好,再加上英语能力差到不行,所以最近半个月学习进度也比较慢, 但还是告诉自己每天都坚持学一点,即使今天心 ...
- SpringMVC入门笔记一
SpringMVC优势 性能比struts2好 简单 便捷 易学 和Spring无缝集成(使用spring ioc aop) 约定优于配置 能够简单进行Junit测试 ...