POJ 1837 Balance (backpack)

Source: Internet
Author: User
Tags integer numbers

Title Link: http://poj.org/problem?id=1837

Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 10983 Accepted: 6824

Description

Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any and ordinary balance.
It orders arms of negligible weight and each arm's length is 15. Some Hooks is attached to these arms and Gigel wants to hang up Some weights from his collection of G weights (1 <= g <=) Knowing that these weights has distinct values in the range 1..25. Gigel may droop any weight of any hooks but he's forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.

Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of Possibilitie s to balance the device.
It is guaranteed, that would exist at least one solution for each test case at the evaluation.

Input

The input has the following structure:
The first line contains the number C (2 <= C <=) and the number G (2 <= G <= 20);
the next line contains C integer numbers (these numbers is also distinct and sorted in ascending order) in the range-15 .. Representing the repartition of the hooks; Each number represents the position relative to the center of the balance in the X axis (when no weights is attached the Device is balanced and lined up to the X axis; The absolute value of the distances represents the distance between the hook and the balance center and the sign of the Nu Mbers determines the arm of the the balance to which the hook was attached: '-' for the left arm and ' + ' for the right arm);
on the next line there is G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the Weights ' values.

Output

The output contains the number M representing the number of possibilities to poise the balance.

Sample Input

2 4-2 3 3 4 5 8

The main topic: there is a balance on both sides have a C hook, G Hook code. It is the number of balance methods to hang all the hooks on the hook. This topic to see the past really is no idea, think about the exam finished six level, patiently read test instructions, but can think of only with search, but 20^20 complexity timeout 0.0

Think for a long time, with the simplest dynamic programming to write. The idea of dynamic planning is that the moment of change of state can be introduced from the first few states. First of all to define a balance of j,j=0 balance, J>0 said the balance right, J<0 said the balance to the left.

Next, define a state array dp[i][j], which indicates the number of hanging methods when the balance is J when I hook code is hung. so each time a hook code is hung, the effect on the balance state is the arm of each hook code

lever = weight * Arm length = w[i]*c[k]; then, if the balance of the Libra is J before hanging the first I, I hook the code, that is, the first I hook code all hanging after the Libra, Libra balance j=j+ w[i]*c[k]

 Special Note : The most extreme situation is that all objects are hung at the far end, so the maximum balance is 15*20*25=7500.  In principle, there should be dp[0..20][-7500. 7500]. So do a deal to make the array open to dp[0.20][0..15000].

See the code.

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intdp[ +][15100];8 9 intMain ()Ten { One     intc,g; A     intc[ +],w[ +]; -      while(~SCANF ("%d%d",&c,&G)) -     { the          for(intI=1; i<=c; i++) -         { -scanf"%d",&c[i]); -         } +          for(intj=1; j<=g; J + +) -         { +scanf ("%d",&w[j]); A         } atMemset (DP,0,sizeof(DP)); -dp[0][7500]=1; -          for(intI=1; i<=g; i++) -              for(intj=0; j<=15000; J + +) -             { -                 //cout<<1111<<endl; in                      for(intk=1; k<=c; k++) -dp[i][j+w[i]*c[k]]+=dp[i-1][j]; to             } +printf ("%d\n", dp[g][7500]); -     } the     return 0; *}



POJ 1837 Balance (backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.