Description (Special Judge)
We are given a sequence of npositive integers A = [a1, a2,..., an] on which we can perform contractionoperations.
One contraction operation consists of replacing Adjacent Elements AI and AI + 1by their difference ai-ai + 1. for a sequence of N integers, We canperform exactly N-1 different contraction operations, each of which results ina new (N-1) element sequence.
Precisely, Let Con (A, I) denote the (N-1) element sequence obtained from [a1, a2 ,..., an] by replacing the elements AI and AI + 1 by a singleinteger ai-ai + 1:
Con (A, I) = [a1,..., ai-1, ai-ai + 1, AI + 2,..., an]
Applying N-1 contractions to any given sequence of N integers obviusly yieldsa single integer.
For example, applying contractions 2, 3, 2 and 1 in that order to the sequence [12, 10, 4, 3, 5] yields 4, since:
Con ([, 5], 2) = [,]
Con ([12, 6, 3], 3) = [12, 6,-2]
Con ([12, 6,-2], 2) = [12, 8]
Con ([12, 8], 1) = [4]
Given a sequence A1, A2,..., an and a target number t, the problem is to finda sequence of N-1 contractions that applied to the original sequence yields T.
Input
The first line of the inputcontains two integers separated by blank character: the integer N, 1 <= n <= 100, the number of integers in the original sequence, and the targetinteger t, -10000 <= T <= 10000.
The following n lines contain the Starting sequence: For each I, 1 <= I <= N, the (I + 1) ST line of the input file contains integer AI, 1 & lt; = ai & lt; = 100.
Output
Output shoshould contain N-1lines, describing a sequence of contractions that transforms the originalsequence into a single element sequence containing only number t. The ith lineof the output file shoshould contain in a single integer denoting the ithcontraction
To be applied.
You can assume that at least one such sequence of contractions will exist for agiven input.
Sample Input
5 4
12
10
4
3
5
Sample output
2
3
2
1
Question Description: give n numbers, for example, 12, 10, 4, 3, and 5. Then, each time you process the position of a number in the sequence, you can use this number to subtract the next number until you get t. [, 3, 5], 2 indicates processing the second number, that is, 10. subtract 4 from 10 to get. [,], 3, indicates processing the third number, that is, 3. Subtract 5 from 3 to get-2. Continue. Until 4 is obtained.
Method: dynamic planning. It can be seen as adding + and-to a string of numbers ,-. Except for the first and second numbers, it must be a minus sign connection. Then process the plus sign. Use F [I] [J] to record the number of I before processing to get J, f [I] [J] memory is obtained by F [I-1] [] is addition or subtraction f [I] [J.
# Include <stdio. h> # include <string. h> # include <stdlib. h> int f [110] [20010], num [110], p [110]; // F [I] [J], the result J is obtained by processing the number of the first I. F [I] [J] is saved by addition or subtraction. Int main () {int N, T; scanf ("% d", & N, & T); For (INT I = 1; I <= N; I ++) {scanf ("% d", & num [I]);} memset (F,-1, sizeof (f); // Add it to 1, minus 0. obviously, the first and second numbers must be reduced by F [1] [num [1] + 10000] = 1; f [2] [num [1]-num [2] + 10000] = 0; For (INT I = 3; I <= N; I ++) {for (Int J = 0; j <= 20000; j ++) {If (F [I-1] [J]! =-1) {f [I] [J + num [I] = 1; F [I] [J-num [I] = 0 ;}}} for (INT I = N, j = T + 10000; I> = 1; I --) {If (F [I] [J] = 1) {P [I] = 1; j-= num [I];} else if (F [I] [J] = 0) {P [I] = 0; J + = num [I] ;}} int COUNT = 0; // process the plus sign first. the plus sign can be regarded as-(B-c) for (INT I = 2; I <= N; I ++) {If (P [I] = 1) {printf ("% d \ n", I-1-count ); // replace num [I] And num [I + 1] With num [I]-num [I + 1. Count ++ ;}for (INT I = 2; I <= N; I ++) {If (P [I] = 0) {printf ("1 \ n"); // I didn't figure out how to output it at first. Later, it was found that after processing the plus sign, the processing order of the minus sign does not matter. You can process the first number each time .}} System ("pause"); Return 0 ;}