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 ...
随机推荐
- SYBASE时间计算
以摘录了计算月初,月末,上月同日,下月同日,上年同日,下年同日(年有闰月问题),各种函数输出格式. 可以写到存储过程中也可单独使用. Sybase日期函数 文章分类:数据库 关键字: sybase日期 ...
- Sharepoint delegate control
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://sch ...
- 各个浏览器下实现Ajax的JS
var xmlhttpget; try { // Firefox, Opera 8.0+, Safari xmlhttpget = new window.XMLHttpRequest( ...
- 简单3d RPG游戏 之 004 攻击(二)
人物和怪物的攻击都有CD冷却,在PlayerAttack脚本中添加成员 //冷却倒计时 public float attackTimer; //CD冷却时间 public float coolDown ...
- [转载]C#缓存absoluteExpiration、slidingExpiration两个参数的疑惑
看了很多资料终于搞明白cache中absoluteExpiration,slidingExpiration这两个参数的含义. absoluteExpiration:用于设置绝对过期时间,它表示只要时间 ...
- 团体程序设计天梯赛-练习集L2-005. 集合相似度
L2-005. 集合相似度 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定两个整数集合,它们的相似度定义为:Nc/Nt*1 ...
- spoj 394
每段可以连续的串的可能性是个Fibonacci数列 但是直接dp更好吧~~ #include <cstdio> #include <cstring> using names ...
- XSS 攻击在它的面前都弱爆了!
虽然双十一刚刚过去不久,但是对很多工程师来说,连续熬夜加班的「噩梦」似乎还没有过去.尤其是像双十一这种活动,对于电商网站的工程师们来说,他们需要彻夜的加班加点来保障网站的稳定性和安全性.当然,面对上千 ...
- C++11新特性:Lambda函数(匿名函数)
声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html 加入了自己的理解,不是简单的翻译 ...
- HTTPS访问:weblogic下配置SSL
进入Weblogic安装路径下的JDK安装目录bin文件下,通过keytool工具生成密钥对(标识密钥库) 输入命令,生成密钥 keytool.exe -genkey -v -alias weblog ...