SUBTRACT
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2037   Accepted: 901   Special Judge

Description

We are given a sequence of N positive integers a = [a1, a2, ..., aN] on which we can perform contraction operations. 
One contraction operation consists of replacing adjacent elements ai and ai+1 by their difference ai-ai+1. For a sequence of N integers, we can perform exactly N-1 different contraction operations, each of which results in a new (N-1) element sequence.

Precisely, let con(a,i) denote the (N-1) element sequence obtained from [a1, a2, ..., aN] by replacing the elements ai and ai+1 by a single integer ai-ai+1 :

con(a,i) = [a1, ..., ai-1, ai-ai+1, ai+2, ..., aN] 
Applying N-1 contractions to any given sequence of N integers obviously yields a single integer. 
For example, applying contractions 2, 3, 2 and 1 in that order to the sequence [12,10,4,3,5] yields 4, since :

con([12,10,4,3,5],2) = [12,6,3,5]

con([12,6,3,5] ,3) = [12,6,-2]

con([12,6,-2] ,2) = [12,8]

con([12,8] ,1) = [4]

Given a sequence a1, a2, ..., aN and a target number T, the problem is to find a sequence of N-1 contractions that applied to the original sequence yields T.

Input

The first line of the input contains two integers separated by blank character : the integer N, 1 <= N <= 100, the number of integers in the original sequence, and the target integer T, -10000 <= T <= 10000. 
The following N lines contain the starting sequence : for each i, 1 <= i <= N, the (i+1)st line of the input file contains integer ai, 1 <= ai <= 100. 

Output

Output should contain N-1 lines, describing a sequence of contractions that transforms the original sequence into a single element sequence containing only number T. The ith line of the output file should contain a single integer denoting the ithcontraction to be applied. 
You can assume that at least one such sequence of contractions will exist for a given input. 

Sample Input

5 4
12
10
4
3
5

Sample Output

2
3
2
1

Source

题意:

给N个数,每次选定一个i,用a[i] - a[i+1]的结果取代a[i]和a[i+1]。操作N-1次之后,就只剩下1个数。问如何选择,可以使这个数恰好是T

思路:

我们可以发现,这个序列每次用减来取代的话,其实就是在n个数之前加上正负,然后让他们都加起来的和结果是T。

而且a[1]一定是正,a[2]一定是负。

于是我们用dp[i][j]来表示第i个数得到分数j时的符号,1表示是加,-1表示是减,0表示没有扩展到。【我怎样才能聪明地想到要这样来表示呢】

我们枚举i,j,判断上一个状态i-1有没有扩展到当前分数j。如果有,那么dp[i][j + a[i]] = 1, dp[i][j - a[i]] = -1

然后我们从dp[n][t]开始倒推出每一个元素之前的符号。ans[i]表示的就是a[i]前的符号。

那么方案该怎么输出?

先去处理所有的加号,相当于把所有的加号都挪去了原来a3的位置,这样最后处理减号的时候,减a2就可以负负得正了。

然后处理减号,把所有的减号都挪到了原来a2的位置,给a1来减。

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n, t;
int a[];
int dp[][], ans[];
const int base = ; int main()
{
while(scanf("%d%d", &n, &t) != EOF){
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
memset(dp, , sizeof(dp));
dp[][a[] + base] = ;
dp[][a[] - a[] + base] = -;
for(int i = ; i <= n; i++){
for(int j = - + base; j <= + base; j++){
if(dp[i - ][j] != ){
dp[i][j + a[i]] = ;
dp[i][j - a[i]] = -;
}
}
} int want = t + base;
for(int i = n; i >= ; i--){
ans[i] = dp[i][want];
if(ans[i] == ){
want -= a[i];
}
else if(ans[i] == -){
want += a[i];
}
} int cnt = ;
for(int i = ; i <= n; i++){
if(ans[i] == ){
printf("%d\n", i - cnt - );
cnt++;
}
}
for(int i = ; i <= n; i++){
if(ans[i] == -){
printf("1\n");
}
}
}
return ;
}

poj1722 SUBTRACT【线性DP】的更多相关文章

  1. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  2. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  3. hdu1712 线性dp

    //Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...

  4. 动态规划——线性dp

    我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...

  5. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

  6. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  7. nyoj44 子串和 线性DP

    线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...

  8. 『最大M子段和 线性DP』

    最大M子段和(51nod 1052) Description N个整数组成的序列a[1],a[2],a[3],-,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M &g ...

  9. 『最长等差数列 线性DP』

    最长等差数列(51nod 1055) Description N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括两项的不 ...

  10. cf909C 线性dp+滚动数组好题!

    一开始一直以为是区间dp.. /* f下面必须有一个s 其余的s可以和任意f进行匹配 所以用线性dp来做 先预处理一下: fffssfsfs==>3 0 1 1 dp[i][j] 表示第i行缩进 ...

随机推荐

  1. JavaScript 学习笔记(二)

    学习内容: 一.变量的定义 二.JS的基本数据类型 三.JS的基本语法 1.变量的定义:   任何语言中最基本的东西就属于变量了,那么如何定义变量以及运用,其实是一件很简单的事情.. JS中提供了如何 ...

  2. xss过滤函数

    XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中. 比如这些代码包括HTML代码和客户端脚本. function remove_xss($ ...

  3. jdbcType 与 Java type

    JDBC Type           Java Type CHAR                String VARCHAR             String LONGVARCHAR      ...

  4. SQLite学习手册

    在实际的应用中,SQLite作为目前最为流行的开源嵌入式关系型数据库,在系统的架构设计中正在扮演着越来越为重要的角色.和很多其它嵌入式NoSQL数据库不同的是,SQLite支持很多关系型数据库的基本特 ...

  5. 用MathType怎么编辑带圈数字序号

    在用MathType编辑公式时,涉及到的数学公式与符号这些都能编辑出来,只要你能够细心一点找到它们相应的模板,不管是在word公式编辑器MathType工具栏模板中,还是在插入符号之后的面板中都可以. ...

  6. 如何解决ChemDraw引起的系统崩溃

    运行ChemDraw应用程序时,一般不会引起系统崩溃,但在使用CS software产品可能会引发计算机崩溃.为了方便广大用户的使用,本教程将教授大家如何解决ChemDraw运行中引起的系统崩溃. 当 ...

  7. python2.0_day18_django_form

    Django formDjango admin 为什么要讲form,Django里的form能做什么. 前面day16节 简单学习了Django admin,我们知道当我们的models在admin. ...

  8. linux添加自启服务(程序)

    修改 /etc/rc.d/rc.local 文件,加入启动程序的脚本命令就可以了 例如: /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongo ...

  9. hadoop程序MapReduce之DataSort

    需求:对文件中的数据进行排序. 样本:sort.log 10 13 10 20 输出:1 10 2 10 3 13 4 20 分析部分: mapper分析: 1.<k1,v1>k1代表:行 ...

  10. eclipse不能自动编译XX.java为XX.classs

    问题描述:eclipse不能自动编译XX.java为XX.classs 原因:今天下午写代码,因为需要引入jstl包,引入后发现原来项目中已经引入了,然后我又把包删除了,忘记删除java build ...