Gym 100703G --- Game of numbers (DP), gym100703g --- game
Question Link
Http://vjudge.net/contest/132391#problem/G
Description
Standard input/output
Statements
-It's a good game,-Princess said pensively. It was clear that she was thinking about something else.
-They like to play various games here in Castles Valley. and they invent ones themselves. say, my friend Knight played with a princess a game some time ago,-Dragon thought it was a good idea o tell Princess about another game, if, perhaps, previous game was seemed no interesting for her.
Princess A. offered Knight to play a game of numbers. She puts down the number zero on a sheet of paper. Let us call this number acurrent result.
Further steps of princess. and Knight are described below. she callany positive integer and Knight says what she must do with this number: to add it to the current result or subtract it from the current result.
Princess A. performs the action and calculates a new value. This value becomes the new current result.
Princess A. wants that current result to be not less than zero and not greaterKAt any time. The game finishes when an action makes the result out of the range or when a sequenceNNumbers, which princess A. conceived, exhausts.
Knight managed to learn the sequenceNNumbers that princess A. guessed, and now he wants the game to last as long as possible.
Your task is to compute maximum possible number of actions which Knight is able to perform during the game.
Input
The first line contains integersNAndK(1 digit ≤ DigitNLimit ≤ limit 1000, limit ≤ 1 limit ≤ limitKLimit ≤ limit 1000)-the size of sequence which princess A. conceived and an upper bound for a current result which must not be exceeded.
The second line containsNIntegersC1, bytes,C2, middle..., middle ,...,CN(1 digit ≤ DigitCJLimit ≤ limitK)-The sequence which princess A. conceived.
Output
In the first line print integerD-Maximum possible number of actions, which Knight is able to perform during the game.
PrintDSymbols "+" and "-" in the second line. SymbolJTh position specifies an action which is appliedJTh number in the princess 'sequence. If multiple answers exist, choose any of them.
Sample Input
Input
2 5
3 2
Output
2
++
Input
5 5
1 2 3 4 5
Output
4
++-+
Enter n and k, and then enter n positive integers (each number is less than or equal to k greater than 0). add or subtract this number from the first number, so that the current formula value ranges from 0 ~ Between k, find the maximum length of this formula,
And output the formula operator;
Idea: DP. Define dp [I] [j] to indicate whether j can be obtained from the number of the first I. If yes, dp [I] [j] = 1; otherwise, it is 0;
The Code is as follows:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cmath>#include <map>#include <vector>using namespace std;int a[1005];char s[1005];bool dp[1005][1005];int main(){ int n,k; while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); dp[1][a[1]]=1; int i,j; for(i=2;i<=n;i++) { int f=0; for(j=0;j<=k;j++) { if(dp[i-1][j]) { if(j+a[i]<=k) { dp[i][j+a[i]]=1; f=1; } if(j>=a[i]) { dp[i][j-a[i]]=1; f=1; } } } if(f==0) break; } printf("%d\n",i-1); s[1]='+'; s[i]='\0'; i--; for(j=0;j<=k;j++) if(dp[i][j]) break; for(;i>=2;i--) { if(j>=a[i]&&dp[i-1][j-a[i]]) { s[i]='+'; j=j-a[i];} else { s[i]='-'; j=j+a[i]; } } puts(s+1); } return 0;}