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 ...
随机推荐
- js笔记——call,apply,bind使用笔记
call和apply obj.call(thisObj, arg1, arg2, ...); obj.apply(thisObj, [arg1, arg2, ...]); 两者作用一致,都是把obj( ...
- Java六大问题你都懂了吗?
这些问题对于认真学习java的人都要必知的,当然如果你只是初学者就没必要那么严格了,那如果你认为自己已经超越初学者了,却不很懂这些问题,请将你自己重归初学者行列. 一.到底要怎么样初始化! 本问题讨论 ...
- Windows Server 2012 R2 里面如何安装Net Framework 3.5
图示 不要慌,和windows是不一样的,没有问题 下一步 默认即可,下一步 这里面的东西以后会装,先不管,我们今天目的是装 net framework 3.5 选一下 正在安装 如果出错了请参考: ...
- Android登录等待效果
上一篇为大家分享了关于AsyncTask的使用,本篇结合AsyncTask为大家介绍一个我们经常看到的一个效果,就是当我们点击登录后,会弹出一个请等待的小窗体,这个效果是如何实现的呢?本篇我就带大家简 ...
- 总结整理 -- 爬虫技术(C#版)
爬虫技术学习总结 爬虫技术 -- 基础学习(一)HTML规范化(附特殊字符编码表) 爬虫技术 -- 基本学习(二)爬虫基本认知 爬虫技术 -- 基础学习(三)理解URL和URI的联系与区别 爬虫技术 ...
- DA - 信息分析思路概要
要素 局部 --->整体 显性 --->隐性 表面 --->本质 割裂 --->联系 特殊 --->普遍 串行 --->并发 纵向 --->横向 单点 --- ...
- Windows Azure Cloud Service (43) 使用Azure In-Role Cache缓存(2)Dedicated Role
<Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...
- [Azure附录]2.在Windows Server 2012中配置AD域服务
<Windows Azure Platform 系列文章目录> 本章我们配置的AD域名为contoso.com 1.安装完AD域服务后,我们返回服务器管理器界面,点击"将此服务器 ...
- 精美素材:40个漂亮的 PSD 贴纸模板《上篇》
贴纸经常被网页设计师用于为标注网站中的不同元素,以此吸引访客的注意.此外,使用贴纸也使网站更具吸引力.虽然设计者可以使用 Photoshop 制作贴纸,但他们也可以利用从互联网上下载现成的模板.这些模 ...
- Javascript动画效果(二)
Javascript动画效果(二) 在前面的博客中讲了简单的Javascript动画效果,这篇文章主要介绍我在改变之前代码时发现的一些问题及解决方法. 在前面的多物体宽度变化的例子中,我们给其增加代码 ...