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 ...
随机推荐
- mac 下nginx加入开机启动
通过brew install nginx后设置开机启动项 sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemonssudo launchc ...
- NodeJS的url信息截取模块url-extract
NodeJS的url信息截取模块url-extract2013-09-12 22:49 by Justany_WhiteSnow, 212 阅读, 0 评论, 收藏, 编辑 上一篇文章,介绍了怎么利用 ...
- 【NET】Winform分页控件初探
public partial class WinFormPager : UserControl { ; /// <summary> /// 当前页 /// </summary> ...
- Android开发效率的小技巧
提高eclipse使用效率(二) 提高Android开发效率的小技巧 XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Wi ...
- Single Image Haze Removal Using Dark Channel Prior
<Single Image Haze Removal Using Dark Channel Prior>一文中图像去雾算法的原理.实现.效果及其他. Posted on 2013-08-2 ...
- Iveely Search Engine 0.4.0 的发布
千呼万唤始出来,Iveely Search Engine 0.4.0 的发布 经过无数个夜晚的奋战,以及无数个夜晚的失眠,Iveely Search Engine 0.4.0 终于熬出来了,这其中 ...
- Lazy<T>
Lazy<T> 对象的创建方式,始终代表了软件工业的生产力方向,代表了先进软件技术发展的方向,也代表了广大程序开发者的集体智慧.以new的方式创建,通过工厂方法,利用IoC容器,都以不同的 ...
- 如何在Ubuntu 11.10上连接L2TP VPN
要在家继续项目的开发,但架设的GitLab只能校内访问,更悲催的是学校架设的SSL VPN不支持Linux,好在想起学校以前架设的L2TP VPN,应该可以支持Linux,于是便一通谷歌百度,然而发现 ...
- MSSQL Rebuild(重建)索引
MSSQL Rebuild(重建)索引 前的项目是做数据库的归档,在每次archive后都需要对原数据库的索引进行rebuild,以减少索引碎片,于是乎就自己写了一段sql: DECLARE @tab ...
- c# 数据库更新操作-文本更新和数值更新小差别
1.文本更新 string strName; sql = "update 模式表 a SET 模式名称 ='"+ strName +"'where a.模式ID =&qu ...