CF GYM 100703G Game of numbers
题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略。
解法:dp。dp[i][j]表示做完第i个数字的运算后结果为j的可能性,可能为1,不可能为0,于是得到状态转移方程dp[i][j] = dp[i - 1][j - a[i]] | dp[i - 1][j + a[i]],当被转移的状态不在0~k之间时特判。dp时记录路径。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
bool dp[1005][1005];
bool path[1005][1005];
int a[1005];
int main()
{
int n, k;
while(~scanf("%d%d", &n, &k))
{
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
memset(dp, 0, sizeof dp);
memset(path, 0, sizeof path);
dp[0][0] = 1;
int ans = 0;
int pos = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= k; j++)
{
if(j - a[i] < 0 && j + a[i] > k)
continue;
if(j - a[i] < 0)
{
dp[i][j] = dp[i - 1][j + a[i]];
path[i][j] = 0;
}
else if(j + a[i] > k)
{
dp[i][j] = dp[i - 1][j - a[i]];
path[i][j] = 1;
}
else
{
dp[i][j] = (dp[i - 1][j - a[i]] | dp[i - 1][j + a[i]]);
if(dp[i - 1][j - a[i]])
path[i][j] = 1;
}
if(dp[i][j])
{
ans = i;
pos = j;
}
}
if(ans != i)
break;
}
printf("%d\n", ans);
vector <bool> v;
for(int i = ans; i > 0; i--)
{
v.push_back(path[i][pos]);
if(path[i][pos])
pos -= a[i];
else
pos += a[i];
}
int len = v.size();
for(int i = len - 1; i >= 0; i--)
{
if(v[i])
printf("+");
else
printf("-");
}
puts("");
}
return 0;
}
CF GYM 100703G Game of numbers的更多相关文章
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF Gym 100685E Epic Fail of a Genie
传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...
- CF gym 101933 K King's Colors —— 二项式反演
题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...
- CF 403D Beautiful Pairs of Numbers
The sequence of integer pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful, if the following state ...
- cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)
题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...
- CF GYM 100703A Tea-drinking
题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
- CF GYM 100703F Game of words
题意:两个人玩n个游戏,给出每人玩每个游戏的时间,两个人需要在n个游戏中挑m个轮流玩,求最短时间. 解法:dp.(这场dp真多啊……话说也可以用最小费用最大流做……然而并不会XD)dp[i][j][k ...
随机推荐
- buffer busy wait在RAC环境下出现
昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...
- 免费web直接打印的控件PAZU
PAZU 是4Fang 四方为配合"四方在线"软件于2004年开发的WEB打印控件,适用于各种WEB软件项目的打印.PAZU是客户端软件,使用于IE作为客户端的所有应用,与服务器端 ...
- iPhone 7-b
iPhone 7就要出了!据悉,苹果秋季新品发布会将于9月7日举行,大家来看看iPhone7的概念设计有多逆天. 新机一出,大家最关心的都是价格问题,那就一起看看大家关注的价格问题: 4.7寸的iPh ...
- 实时数据处理环境搭建flume+kafka+storm:1.zookeeper 安装配置
1. 解压 tar -zxvf 2.创建目录 zk根目录创建 mkdir zkdatalog --日志 mkdir zkdata ---快照文件 3.修改配置文 ...
- ExtJS4.2学习(12)基于表格的右键菜单(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-24/181.html --------------- ...
- PHP MSSQL数据操作PDO API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 看几道JQuery试题后总结(下篇)
感谢圆友的提醒 昨天下午完成了9道试题中的前4道,之后好多园友存在些疑惑和建议,在这里我一并说一下吧.首先对于昨天第一题可能存在误导,在JQuery中并没有innerHTML这个属性,不过我们可以将J ...
- android 使用Activity做窗口弹出(模拟Dialog)
我们下面使用Activity,模拟一个dialog: 首先看布局: <?xml version="1.0" encoding="utf-8"?> & ...
- GridView中DataKeyNames的应用小结
一. GridView的DataKeyNames属性设为"ID,Name" GridView1.DataKeyNames = new string[]{ "ID" ...
- HeadFirst设计模式之装饰者模式
一. 1.The Decorator Pattern attaches additional responsibilities to an object dynamically.Decorators ...