Gym 100703G---Game of numbers(DP)
题目链接
http://vjudge.net/contest/132391#problem/G
Description
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 A. and Knight are described below. She calls any 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 greater than k at any time. The game finishes when an action makes the result out of the range or when a sequence of n numbers, which princess A. conceived, exhausts.
Knight managed to learn the sequence of n numbers 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 integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ 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 contains n integers c1, c2, ..., cn (1 ≤ cj ≤ k) — the sequence which princess A. conceived.
Output
In the first line print integer d — maximum possible number of actions, which Knight is able to perform during the game.
Print d symbols "+" and "-" in the second line. Symbol at jth position specifies an action which is applied to jth number in the princess' sequence. If multiple answers exist, choose any of them.
Sample Input
2 5
3 2
2
++
5 5
1 2 3 4 5
4
++-+ 题意:输入n,k 然后输入n个正整数(每个数小于等于k 大于0)从第一个数开始加上或者减去这个数,使得当前的算式值在0~k之间,求这个算式的最大长度,
并输出这个算式的运算符; 思路:DP,定义dp[i][j]表示由前i个数能否得到j,能则dp[i][j]=1,否则为0; 代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
int a[];
char s[];
bool dp[][]; int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
dp[][a[]]=;
int i,j;
for(i=;i<=n;i++)
{
int f=;
for(j=;j<=k;j++)
{
if(dp[i-][j])
{
if(j+a[i]<=k) { dp[i][j+a[i]]=; f=; }
if(j>=a[i]) { dp[i][j-a[i]]=; f=; }
}
}
if(f==) break;
}
printf("%d\n",i-); s[]='+'; s[i]='\0'; i--;
for(j=;j<=k;j++)
if(dp[i][j]) break; for(;i>=;i--)
{
if(j>=a[i]&&dp[i-][j-a[i]]) { s[i]='+'; j=j-a[i];}
else { s[i]='-'; j=j+a[i]; }
}
puts(s+);
}
return ;
}
Gym 100703G---Game of numbers(DP)的更多相关文章
- URAL 1586 Threeprime Numbers(DP)
题目链接 题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数. 求对于一个n位数,存在多少个Threeprime数. 思路 : 记录[100, 999]范围内所有素数( ...
- POJ1338Ugly Numbers(DP)
http://poj.org/problem?id=1338 第一反应就是DP,DP[i] = min{2*DP[j], 3*DP[k], 5*DP[p] j,k,p<i};于是枚举一下0-i- ...
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...
- 【Gym - 101002F】Mountain Scenes(dp)
Mountain Scenes Descriptions 给你一个长度为n的丝带,一个宽w一个高h 的 格子,用丝带去填充格子,这填充后只需要满足至少有一列的丝带长度与其他格子不同即可.丝带可以不全部 ...
- 【gym102394B】Binary Numbers(DP)
题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...
- 【CF55D】Beautiful numbers(动态规划)
[CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
随机推荐
- OutputCache属性详解(三)— VaryByHeader,VaryByCustom
目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...
- javascript_basic_02之数据类型、分支结构
1.弱类型:声明无需指定数据类型,由值决定,查看变量数据类型:typeof(变量): 2.隐式转换:任何数据类型与string类型相加,结果为string类型: 3.显式(强制)转换: ①toStri ...
- 简述移动端IM开发的那些坑:架构设计、通信协议和客户端
1.前言 有过移动端开发经历的开发者都深有体会:移动端IM的开发,与传统PC端IM有很大的不同,尤其无线网络的不可靠性.移动端硬件设备资源的有限性等问题,导致一个完整的移动端IM架构设计和实现都充满着 ...
- iOS $299刀企业证书申请的过程以及细节补充(二)
上篇博客写的过程中,没有图,也没有相应的说明.这次再补充一些信息: 1.从 https://developer.apple.com/ios/enroll/dunsLookupForm.action 申 ...
- 查看Query Plan
在执行一个查询语句时,查询优化器编译查询语句,产生一个足够好的Compiled Plan,将其缓存到plan cache中.Compiled plan是基于batch的,如果一个batch含有多个qu ...
- CSS 框模型( Box module )
框和布局 在 KB005: CSS 层叠 中已经介绍了 CSS 的重要之处.CSS 可以说是页面表现的基础, CSS 可以控制布局,控制元素的渲染. 布局是讲在电影画面构图中,对环境的布置.人物地位的 ...
- poj2513Colored Sticks(无向图的欧拉回路)
/* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...
- 使用NPOI从Excel中提取图片及图片位置信息
问题背景: 话说,在ExcelReport的开发过程中,有一个比较棘手的问题:怎么复制图片呢? 当然,解决这个问题的第一步是:能使用NPOI提取到图片及图片的位置信息.到这里,一切想法都很顺利.但NP ...
- php分享(三十六)mysql中关联表更新
一:关联不同的表更新 1: 通过where关联更新 update student s, city c set s.province_name = c.province_name, s.city_nam ...
- ORA-32004
今天在启动数据库的过程中,收到以下错误: SQL> startup ORA: obsolete or deprecated parameter(s) specified for RDBMS in ...