Blue Bridge Cup training algorithm questions stamps

Examination training algorithm stamps

Resource constraints
Time limit: 1.0s memory limit: 512.0MB
problem description
  given an envelope with N (1≤N≤100) positions can stamp, each location can only bear a stamp. We now have M (M <= 100) different postage stamps, in denominations of X1, X2 ... .Xm points (Xi is an integer, 1≤Xi≤255), each with N Zhang.

Obviously, the minimum value of the postage on an envelope that can be affixed min (X1, X2, ..., Xm), the maximum value is N * max (X1, X2, ..., Xm). All postage value obtained by the paste method may form one set (there are no duplicate set value), if required to obtain the maximum value from 1 to a continuous sequence of postage value, the output sequence is present in this set.

For example, N = 4, M = 2 , denominations of 4 points, 1 point, thus forming the sequence 1,2,3,4,5,6,7,8,9,10,12,13,16, and continuous sequence starting from the postage is 1,2,3,4,5,6,7,8,9,10, the maximum value of postage continuous sequence of 10 minutes.
Input format
  First line: Maximum number of sheets of stamps pasted N; second line: the number M kinds of stamps; Third row: M digital space separated, Xi stamp indicating the denomination value. Note: Xi is not necessarily the size of the ordered sequence!
Output format
  from a continuous sequence of a postage starting maximum value MAX. Starting from a sequence of points if the presence of (i.e. not inputted stamp denominations at 1 minute), the output 0.
Sample Input
Sample a:
. 4
2
. 4 1
Sample II:
10
. 5
2. 8. 6 10. 4

Sample Output

Sample a:
10
Sample II:
0

Thinking: This question is if you do not master the method to calculate it can be very complicated, a lot more unnecessary calculations, and the answer is not correct, it is important to choose the right method. First, we can be the first number stored, the ability to meet the conditions, when the first number is 1 matches, then have each calculated from the number of how many positions need to take everything down with an array of convenient on the next calculation , and can save a lot of calculation steps and fast, accurate, each time subtracting this number on how many stamps and how many positions need to occupy available (such as the title had a 11-4 record before = 7,7, so the need to make up 7 and a 4 1 three, plus 11 need to add a 4, occupies a position, so that the title is greater than 5 n = 4, does not meet the conditions), does not meet the conditions for determining the position of the stamp occupied by more than a given position or that no sequence numbers to the numbers in the given range is the stamp position is not eligible.

code show as below:

#include<iostream>
#include<string.h>
using namespace std;
int main(){
	int a[101],b[255001],c,i,j,n,m;  //255001为可能出现的最大值
	memset(b,0,sizeof(b));
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>a[i];
	}
	for(i=0;i<m;i++){           //给邮资排序 
		for(j=0;j<m;j++){
			if(a[i]<a[j]){
				int t;
				t=a[i];a[i]=a[j];a[j]=t;
			} 
		}
	}
	for(j=1;j<=a[m-1]*n;j++){  //最大值即为最大的数乘n
		for(i=m-1;i>=0;i--){
			if((b[j]==0 || b[j-a[i]]<b[j]) && j>=a[i]){  //b[j-a[i]]<b[j]选取最少的占用位置数
				b[j]=b[j-a[i]]+1;
			}
		} 
		if(b[j]==0 || b[j]>n){
			cout<<j-1;
			break;
		}
	}
}
Published 51 original articles · won praise 47 · views 2010

Guess you like

Origin blog.csdn.net/weixin_45269353/article/details/104557530