poj1722 SUBTRACT
应该是基础的dp练手题
线性dp最主要的就是关于阶段的划分,这个题中我没想到的一点就是开状态的时候使用了前i个数能合成的数来记录
我自己的想法就是类似于区间dp这样的记录方法,这种方法确实开了很多冗余的状态而造成了整道题卡住……
题目中的添减号和括号,实际上可以转化为添加号,这样子最后输出的时候就可以通过把加号换成括号和减号来进行输出。还好spj。
Code:
#include <cstdio>
#include <cstring>
using namespace std; const int N = ;
const int T = ;
const int Trans = ; int n, t, a[N];
char dp[N][T], op[N]; int main() {
scanf("%d%d", &n, &t);
t += Trans;
for(int i = ; i <= n; i++)
scanf("%d", &a[i]); memset(dp, , sizeof(dp));
dp[][a[] - a[] + Trans] = '-';
for(int i = ; i < n; i++)
for(int j = ; j < (Trans << ); j++)
if(dp[i][j] != ) {
if(j + a[i + ] <= (Trans << )) dp[i + ][j + a[i + ]] = '+';
if(j - a[i + ] >= ) dp[i + ][j - a[i + ]] = '-';
} for(int i = n; i >= ; i--) {
op[i] = dp[i][t];
if(dp[i][t] == '+') t -= a[i];
else t += a[i];
} int cnt = ;
for(int i = ; i <= n; i++)
if(op[i] == '+') {
printf("%d\n", i - cnt - );
cnt++;
}
for(int i = ; i <= n; i++)
if(op[i] == '-') puts(""); return ;
}
poj1722 SUBTRACT的更多相关文章
- poj1722 SUBTRACT【线性DP】
SUBTRACT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2037 Accepted: 901 Special ...
- 常规DP专题练习
POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...
- 题解 【POJ1722】 SUBTRACT
先讲下题目意思 给定一个长度为\(n\)的序列\((1 \leq n \leq 100)\), 每次合并两个元素\(i,i+1\),即将\(i,i+1\)变为一个新的元素,权值为\(a[i]-a[i+ ...
- RDD常用方法之subtract&intersection&cartesian
subtract Return an RDD with the elements from `this` that are not in `other` . def subtract(othe ...
- [CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...
- POJ1722 动态规划
POJ1722 问题重述: 给定一个数组a[1,2,..,n] .定义数组第i位上的减操作:把ai和ai+1换成ai - ai+1.输入一个n位数组以及目标整数t,求一个n-1次操作序列,使得最后剩下 ...
- add, subtract, multiply, divide
加.减.乘.除:add, subtract, multiply, divide
- MongoDB比较两列大小 使用$subtract函数
是找出整个表 a大于b的总数量,要怎么操作数据库呢,那就要用到$subtract函数 MongoDB比较两列大小 使用$subtract函数, // MongoDB 比较两列大小求出 啊>b 的 ...
- Spark 学习笔记之 union/intersection/subtract
union/intersection/subtract: import org.apache.spark.SparkContext import org.apache.spark.rdd.RDD im ...
随机推荐
- SQL Server: Top 10 Secrets of a SQL Server Expert
转载自:http://technet.microsoft.com/en-us/magazine/gg299551.aspx Many companies have downsized their IT ...
- 洛谷 P1292 倒酒
题目描述 Winy是一家酒吧的老板,他的酒吧提供两种体积的啤酒,a ml和b ml,分别使用容积为a ml和b ml的酒杯来装载. 酒吧的生意并不好.Winy发现酒鬼们都非常穷.有时,他们会因为负担不 ...
- minio nginx 配置
1. 参考配置 server { listen 80; server_name example.com; location / { proxy_set_header Host $http_host; ...
- vscode: Visual Studio Code 常用快捷键【轉】
主命令框 F1 或 Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以输入任何命令,例如: 按一下 Backspace 会进入到 Ctrl+P 模式 在 Ctrl+P 下输入 > ...
- React组件传值方式总结
1. 子组件向父组件传值 父组件Header: import Nav from 'Nav.js'; class Header extends React.Component { constructor ...
- python使用wxPython创建一个简单的文本编辑器。
ubuntu下通过'sudo apt-get install python-wxtools'下载wxPython.load和save函数用于加载和保存文件内容,button通过Bind函数绑定这两个函 ...
- fineUI表格控件各属性说明
最近在新的公司后台用fineUI,所以也稍微总结一下表格的一些属性,希望对刚入手的人有些帮助: AllowPaging 允许分页,为true时表示允许分页,表格下方会出现分页工具栏: PageSize ...
- 洛谷 P3302 [SDOI2013]森林 Lebal:主席树 + 启发式合并 + LCA
题目描述 小Z有一片森林,含有N个节点,每个节点上都有一个非负整数作为权值.初始的时候,森林中有M条边. 小Z希望执行T个操作,操作有两类: Q x y k查询点x到点y路径上所有的权值中,第k小的权 ...
- php代码优化 -- array_walk 和 foreach, for 的效率的比较
<?php /** * array_walk 和 foreach, for 的效率的比较. * 我们要测试的是foreach, for, 和 array_walk的效率的问题. */ //产生一 ...
- oracle创建表空间、创建用户
create user user_name identified by user_name create temporary tablespace user_name_temp tempfile '/ ...