poj 1293 Duty Free Shop

Duty Free Shop

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1145   Accepted: 452   Special Judge

Description

Pedro travelled to Europe to take part in the International Olympiad in Informatics and is coming back home. Since all his friends asked him to bring them some gift, he bought two big bags of chocolates (one of Mindt and one of Lilka). Each of these two bags
contains a certain number of small chocolates. Buying those two bags was much less expensive than buying smaller, individual boxes of chocolates. At home, Pedro has some empty chocolate boxes that he kept from other trips. Pedro intends to distribute the chocolates
he just bought into these smaller boxes, to give them to his friends.

As soon as Pedro begins filling the small boxes, he realizes he has a big problem: since he has two different brands of chocolates, if he mixes chocolates of different brands into one small box, the friend who receives this small box will discover Pedro‘s trick
to save money, and will not be pleased with him.

You must help poor Pedro distribute the chocolates into the small boxes in such a way that every small box is completely full, and contains only one brand of chocolates. A number of chocolates may however be left unassigned to any box (Pedro will keep these
chocolates to himself).

Input

The input contains several instances of the problem. Each instance consists of three lines. The first line contains two integers M and L that indicate respectively the number of chocolates Mindt and Lilka Pedro bought (0 <= M, L <= 1000). The next line contains
an integer N representing the number of small boxes Pedro has (N <= M+L). The third line contains N integers indicating the capacity Ci > 0 of box number i (that is, the number of chocolates needed to fill that box). The end of input is indicated by M = L
= 0.

Output

For each instance of the input your program must produce one line of output. If it is possible to distribute the chocolates as defined in the problem statement, print the number of boxes to be filled with Mindt chocolate, followed by a space, followed by the
list of box numbers, in ascending order. Each box number in the list should be followed by a space. If it is impossible to distribute the chocolates,print "Impossible to distribute". If more than one solution exists, print any one.

Sample Input

12 9
4
5 2 8 5
100 120
5
21 32 110 54 3
0 0

Sample Output

3 1 2 4
Impossible to distribute

题意:

给m块A巧克力,l块B巧克力,要把这些巧克力装进一些小包,要保证每个小包只有一种类型的巧克力,输出装A巧克力的小包的总和序号。

题解:

只处理A巧克力就行了,当A放在包里的最多时,B能把包放满,那么就可行,这样就变成裸的01背包了。

不让输出方案倒很容易想到,让输出方案要怎么解决呢?想了好久,还是看了别人的思路,用一个数组pre[i]存dp[i]最大时最后放到dp[i]中的小包序号,然后再dfs找dp[m]的所有放置序列,由于让从小到大输出小包的序号,可以用个vis数组记录最后的序号。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000+100;
int a[maxn],dp[maxn],pre[maxn];
bool vis[maxn];
int n,m,l;
void dfs(int x)
{
    if(x==0)
    return;
    vis[pre[x]]=1;
    dfs(x-a[pre[x]]);
}
int main()
{
    while(~scanf("%d%d",&m,&l)&&l+m)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
             scanf("%d",&a[i]);
        }
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=a[i];j--)
            {
                if(dp[j]<dp[j-a[i]]+a[i])
                {
                    dp[j]=dp[j-a[i]]+a[i];
                    pre[j]=i;
                }
            }
        }
        int ans=n;
        dfs(dp[m]);
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
               ans--;
               sum+=a[i];
            }
        }
        if(sum<=l)
        {
        printf("%d",ans);
        for(int i=1;i<=n;i++)
        {
            if(vis[i])
            printf(" %d",i);
        }
        printf("\n");
        }
        else
        {
            printf("Impossible to distribute\n");
        }
    }
    return 0;
}
时间: 2024-10-14 11:00:32

poj 1293 Duty Free Shop的相关文章

POJ 1293 Duty Free Shop(背包记录路径)

Description Pedro travelled to Europe to take part in the International Olympiad in Informatics and is coming back home. Since all his friends asked him to bring them some gift, he bought two big bags of chocolates (one of Mindt and one of Lilka). Ea

POJ 题目1157 LITTLE SHOP OF FLOWERS(DP)

LITTLE SHOP OF FLOWERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19457   Accepted: 8966 Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different

zoj 1520 - Duty Free Shop

题目:Pedro买了两块不同牌子的巧克力,他找到了一些小盒子,他准备把巧克力分开,放在小盒子里送给朋友: 为了不被朋友发现自己是为了省钱,每个小盒子中只能放相同牌子的巧克力,求分法. 分析:dp,01背包. 这里每个小盒子作为物品,其中第一块巧克力作为箱子,记录每个箱子的前驱(路径): 然后,枚举所有的第一块巧克力的可分状态,判断第二块巧克力是否可以装下剩下的小盒子: 找到合法的情况,逆序输出路径即可. 说明:当时zoj的第50道 dp O(∩_∩)O~ #include <stdio.h>

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

English trip -- VC(情景课) 7 B Clothing 服装

xu言: 不要使用中式的思维去思考西方的语义!!!切记切记 words a tie   领带 a blouse  女士衬衣 a sweater  毛衣 a skirt  短裙 a jacket   夹克衫(一般还用于指上半身穿的衣服) a raincoat 雨衣 Duty Free Shop (简称:DFS)免税商店 tax   n. 税金:重负 freight  运费 sneakers    s nea kers  ['sni:k?z]   休闲鞋 Sentences How much is