New Year's Fun-Card Game Solution

New Year's Fun: Card Game

Title description

During the New Year, the favorite activity of adults is playing cards. Xiaomengxian didn't know how to play cards, so he had to sit and watch. On this day, just as a group of people were able to play cards vigorously, someone suddenly shouted: "This deck of cards is missing a few cards!" Everyone counted them, and it was indeed missing. So the owner of this deck proudly said: "This is a special card. I know the weight of each card in the entire deck. As long as we weigh the total weight of the remaining cards, we can know which cards are missing. "Everyone thought this method was good, so they weighed the total weight of the remaining cards and started to calculate which cards were missing. Due to the relatively large amount of data, after a while, everyone felt dizzy. At this time, Xiaomengxian said loudly: "Look at me!" So he took out his laptop, compiled a program, and quickly found the missing cards. What if you encountered such a situation? Can you do the same thing?

Input format

An integer TotalW in the first line represents the total weight of the remaining cards. In the second line, an integer N (1<n<=100) indicates how many cards there are in this deck. Next N lines, each line has an integer wi (1<=wi<=1000), which represents the weight of each card.

Output format

If there is no solution, output "0"; if there are multiple solutions, output "-1"; otherwise, output the numbers of the missing cards in ascending order, separated by a space between two adjacent numbers.

Input sample

270
4
100
110
170
200

Sample output

2 4

This is a problem similar to the 01 backpack. Just make up the remaining total weight of the plan. Finally, note that the output is not the made up plan, but the output. Pay attention to record the plan.

Note that the given volume is not what we want. You need to subtract the given volume from the total volume.

We open an array to record the decision, and only when the number of plans of the current volume is 0, we record the decision. (Think about why)

The output is the recursive output, which is similar to the one written yesterday. It is also the general basic method for the output of the backpack problem.

Note that the number of schemes may be very large, so we add an optimization sentence. If the number of schemes is greater than 10000, it becomes the value of mod 10000. Since we only need to make a judgment, this will not affect the result.
Let dp[i] dp[i]d p [ i ] indicatesiwhether it is feasible when theweight is, and the knapsack transfer is sufficient, but it is necessary to consider the situation of no solution and multiple solutions. No solution property is 0, and multiple solution property is -1. Finally, judge if dp [TotalW]<=0 means no solution and multiple solutions, output 0 or -1 directly, and output the current solution in other cases.

#include <bits/stdc++.h>
#define MAXN 105
#define MAXM 10005
using namespace std;
int w[MAXN], vis[MAXN];
int dp[MAXM], nxt[MAXM];
int main()
{
    
    
    memset(dp, 0, sizeof(dp));
    int TotalW, n;
    scanf("%d %d", &TotalW, &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &w[i]);
    dp[0] = 1;
    for(int i = 1; i <= n; i++)
    {
    
    
        for(int j = TotalW; j >= w[i]; j--)
            if(dp[j - w[i]])
                if(dp[j] == 0)
                    dp[j] = 1, nxt[j] = i;
                else
                    dp[j] = -1;
            else if(dp[j - w[i]] == -1)
                dp[j] = -1;
	}
	if(dp[TotalW] <= 0)
		printf("%d\n", dp[TotalW]);
    else
    {
    
    
	    int s = TotalW; 
	    while(s)
	    {
    
    
	        vis[nxt[s]] = 1;
	        s = s - w[nxt[s]];
	    }
	    for(int i = 1; i <= n; i++)
	    	if(!vis[i]) 
	    		printf("%d\n", i); 
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/CoderZeng/article/details/109053505
Recommended