1090. [Vijos 1071] New Year fun of playing cards

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#define LL  long long
using namespace std;
int we,n,a[1050],i,j,dp[110005],num[101000],ans[1050]={0};//变量常量说明
void init()
{
  cin>>we>>n;
  for(i=1;i<=n;i++)
   cin>>a[i];
  memset(dp,0,sizeof(dp));
  dp[0]=1;
}
void solve()
{
    for(j=1;j<=n;j++)
    for(i=we;i>=a[j];i--)
     if(dp[i-a[j]])
      {
          dp[i]+=dp[i-a[j]];//容积i时方案数
          if(num[i]==0)
          num[i]=j;//记录途径
      }
    if(dp[we]==0)
      cout<<"0"<<endl;
    if(dp[we]>1)
      cout<<"-1"<<endl;
    if(dp[we]==1)
    {
        int v=we;
        while(v)
        {
            ans[num[v]]=1;//标记
            v-=a[num[v]];
        }
     for(i=1;i<=n;i++)
       if(ans[i]==0)
          cout<<i<<' ';

    }

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

 

 

Chinese New Year, adults favorite activity is playing cards. xiaomengxian not play cards, but to sit on the sidelines watching.
That day, just a group of people playing cards to play enthusiastically, when suddenly someone shouted: "! The deck a few less" a number of people, it really is less. So the deck of the owner proudly said: "This is a special card, I know the whole deck each one as long as we weigh the weight of the total weight of the rest of the cards, which will be able to know little card. . "we all feel this way good, so weigh out the rest of the total weight of the card, which counted fewer cards. Due to large amount of data, before long, we have considered dizzy.
At this time, xiaomengxian exclaimed: "You look at me!" So he took out a notebook, to compile a program that quickly put the missing license to find out.
If you encounter such a situation? You can do the same thing?

[Input Format]

The first line an integer TotalW, represents the total weight of the remaining cards.
The second line of an integer N (1 <= N <= 100) <n <= 100), indicates the number of sheets of the deck. <Span = "">
Next N rows, each row an integer Wi (1 <= Wi <= 1000), represents the weight of each card. </ n <= 100), indicate how much of the deck sheets. <>

[Output format]

If no solution, output "0"; if several solutions, output "-1"; otherwise, in ascending order of output lost card numbers, separated by a space between adjacent two numbers.

[Sample input]

270

4

100

110

170

200

[Sample output]

2 4

********************************************************************************************
The problem is deformed knapsack problem, this problem may be provided an array DP [v], expressed as knapsack capacity v number scheme, denoted by dp [0] = 1; then an array path [i] represents the volume i the numbers put when the backpack, and finally with a while loop flag when v == n backpack by the release number when, if dp [n] == 1 , the output is not marked packets back label number; if dp [n] == 0, the corresponding output value, if dp [n]> 1 , the corresponding output value.
*********************************************************************************************************************

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3155233.html

Guess you like

Origin blog.csdn.net/weixin_34392906/article/details/93433002