Analysis for poj 3021

1、 Title

(1) Currency has traditional value and IT value. When it is converted into new currency, the sum of the squares of traditional value and IT value is equal to the sum of the squares of new currency.

(2) Given m kind of currencies, each of which contains traditional value a and it value B, each currency can be selected multiple times, so that the sum of traditional value and it value can be obtained.

(3) Given the number of new currencies needed, ask if it is possible to convert the m kind of currencies given above into new currencies in the following ways: traditional currency and ^ 2 + IT currency and ^ 2 = new currency ^ 2. If it can’t, output impossible, if it can, output the number of old currencies needed.

Input:

The number of questions is n (0 < n < = 100), followed by N times of the following data:

  1. M (0 < m < = 40) and S (0 < s < = 300), M represents the type of old currency, and s represents the number of new currencies to be exchanged.

  2. M-line two tuples, indicating that there are m kinds of old currencies, each of which is described by a pair of traditional values and IT values.

Output: n lines, each containing an integer, the number of old currencies needed to convert s new currency, or “not possible”.

2、 Problem solving ideas (blog content)

1, DP algorithm

(1) The method of DP is an exhaustive method;

(2) DP is constantly dynamic optimization when it exhausts all possible, that is to say, whether the old results are improved according to the new calculation results or not, if there is improvement, it will be replaced by the current optimum;

(3) After all possibilities are calculated, it becomes the ultimate optimum.

  1. Steps for DP to solve this problem:

(1) DP [i] [j] is defined as: the number of old currencies needed when i traditional value and j information value are selected from m kind of old currencies;

(2) The optimal algorithm is dynamic transfer equation;

(3) For each i or j from 0 to s, the number of old currencies required to make up this (i, j) is calculated;

(4) Find out all (i, j) that meet the exchange conditions: i ^ 2 + j ^ 2 = = s ^ 2, and the minimum DP [i] [j] is required.

  1. The advantage of DP method is that it simplifies the algorithm with the cost of calculation, that is to say, it only needs to find a simple state transition equation to carry out the calculation. The cost of DP is a little high, and all that can’t meet the exchange conditions have been calculated.

  2. Are there any other solutions:

(1) You can find all the combinations of (I, J) that satisfy s ^ 2 = I ^ 2 + J ^ 2;

(2) For each group (I, J), the loop looks for the number of tuples (I, J) that can make up this (I, J) at the same time. However, there may be many loops, such as m-loop (step-by-step problem), for example, tuple 1 from 0 to s, tuple 2 from 0 to s From the complexity of calculation, it seems to be more complex than DP.

3、 Take an example to understand the meaning of the question:

(1) Eight (2, 1) and two (0, 2), then the traditional value is 8 × 2 + 2 × 0 = 16, the IT value is 8 × 1 + 2 × 2 = 12, 16 ^ 2 + 12 ^ 2 = 20 ^ 2; therefore, the required number of tuples is 10;

(2) One (3, 0) and one (0, 4), then the traditional value is 1 × 3 + 1 × 0 = 3, the IT value is 1 × 0 + 1 × 4 = 4, so s = 5; therefore, the required number of tuples is 2;

4、 Analysis :

  1. Question: know m kinds (a, b), they need to exchange s new coins, ask, in the possible exchange combination, what is the minimum number of tuples.

  2. Traditional value and IT value exchange s new currency, there should be a variety of exchange combinations, we find out these combinations, respectively calculate their corresponding number of tuples, and then output the least;

  3. Define problem: let DP (i, j) store the minimum number of tuples (a, b) through which there are I traditional values and j it values. Then the problem i s that if such I and J can just exchange s (satisfying i ^ 2 + j ^ 2 = s ^ 2), the minimum of all such I and j is what DP (i, j) is.

  4. State transition: for each DP (i, j), we judge each tuple (a, b) in turn. If the total I traditional value and j IT value are used, then a traditional currency is added, and B it value is added, that is, it is obtained from DP (i-A, j-B) + 1. Otherwise, DP (i, j) is unchanged. So, the equation of state transition is: DP [i] [j] = min (DP [i] [j], DP [i-A [k]] [j-B [k]] + 1);

  5. At first, all DP s (i, j) are very large values, DP (0,0) = 0. Then, the dynamic chip lists all the values from DP (0,0) to DP (s, s). Its calculation method is to traverse all the tuples for each i and j, to see whether the number of tuples constructed into I and J will change (whether there are smaller ones).

Because i ^ 2 + j ^ 2 = s ^ 2, we can enumerate all possible combinations of s from 1 to s and j from 1 to s. Therefore, if all i’s are from 1 to s and j’s are from 1 to s, we traverse all the tuples in turn to see if there is a tuple that makes DP (i, j) smaller, which means that there is a better way to make up i traditional coins and j it coins.

  1. For the combination of i and j that can satisfy the exchange condition, find the least number of tuples as the output result.

The followings are my code:
#include
#include
#include
using namespace std;
const int inf=1<<28;
int a[102],b[102],dp[302][302];
int main()
{
int n;
scanf("%d",&n);
while(n–)
{
int m,s;
scanf("%d%d",&m,&s);
int i,j,k;
for(i=0;i<m;i++)
{
scanf("%d%d",&a[i],&b[i]);
}
for(i=0;i<=s;i++)
{
for(j=0;j<=s;j++)
{
dp[i][j]=inf;
}
}
dp[0][0]=0;
for(i=0;i<=s;i++)
{
for(j=0;j<=s;j++)
{
for(k=0;k<m;k++)
{
if(i>=a[k] && j>=b[k])
{
dp[i][j]=min(dp[i][j],dp[i-a[k]][j-b[k]]+1);
}
}
}
}
int ans=inf;
for(i=0;i<=s;i++)
{
for(j=0;j<=s;j++)
{
if(ii+jjs*s)
{
ans=min(ans,dp[i][j]);
}
}
}
if(ans
inf)
{
printf(“not possible\n”);
continue;
}
printf("%d\n",ans);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/alex_yuqian/article/details/105132843
今日推荐