Duty Free Shop - POJ 1293 dp

Duty Free Shop
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1132 Accepted: 444 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

思路:滚动数组记录放第n个前的可能的状态,然后再递推。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int t=0,dp[2][1003000],num[2010],ans[2010],match[1003000];
void solve(int pos,int from,int to)
{ int a,b,i,j,k;
  dp[to][0]=0;
  for(i=1;i<=dp[from][0];i++)
  { a=dp[from][i]/1001;
    b=dp[from][i]%1001;
    if(a>=num[pos])
    { k=(a-num[pos])*1001+b;
      if(match[k]!=t*2 && match[k]!=t*2+1)
      { match[k]=t*2+1;
        dp[to][0]++;
        dp[to][dp[to][0]]=k;
      }
    }
    if(b>=num[pos])
    { k=a*1001+b-num[pos];
      if(match[k]!=t*2 && match[k]!=t*2+1)
      { match[k]=t*2;
        dp[to][0]++;
        dp[to][dp[to][0]]=k;
      }
    }
  }
}
int main()
{ int n,a,b,i,j,k,p;
  while(~scanf("%d%d",&a,&b) && a+b)
  { if(a>1000 || b>1000)
     while(true){}
    t++;
    dp[0][0]=1;
    dp[0][1]=a*1001+b;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
     scanf("%d",&num[i]);
    for(i=1;i<=n;i++)
    { if(i&1)
       solve(i,0,1);
     else
       solve(i,1,0);
    }
    if(dp[n&1][0]==0)
    { printf("Impossible to distribute\n");
      continue;
    }
    k=dp[n&1][1];
    ans[0]=0;
    for(i=n;i>=1;i--)
    { if(match[k]==t*2+1)
      { ans[0]++;
        ans[ans[0]]=i;
        k+=num[i]*1001;
      }
      else
       k+=num[i];
    }
    printf("%d",ans[0]);
    for(i=ans[0];i>=1;i--)
     printf(" %d",ans[i]);
    printf("\n");
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值