poj1722 SUBTRACT【线性DP】
| Time Limit: 1000MS | Memory Limit: 10000K | |||
| Total Submissions: 2037 | Accepted: 901 | Special Judge | ||
Description
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 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
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】的更多相关文章
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- 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门 ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- nyoj44 子串和 线性DP
线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...
- 『最大M子段和 线性DP』
最大M子段和(51nod 1052) Description N个整数组成的序列a[1],a[2],a[3],-,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M &g ...
- 『最长等差数列 线性DP』
最长等差数列(51nod 1055) Description N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括两项的不 ...
- cf909C 线性dp+滚动数组好题!
一开始一直以为是区间dp.. /* f下面必须有一个s 其余的s可以和任意f进行匹配 所以用线性dp来做 先预处理一下: fffssfsfs==>3 0 1 1 dp[i][j] 表示第i行缩进 ...
随机推荐
- 用 #include “filename.h” 格式来引用非标准库的头文件
用 #include “filename.h” 格式来引用非标准库的头文件(编译器将 从用户的工作目录开始搜索) #include <iostream> /* run this progr ...
- php -- 读取大文件
在PHP中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数,简简单单的几行代码就能 很漂亮的完成我们所需要的功能.但当所操作的文件是一个比较大的 ...
- Uniprot 数据库-最常用的蛋白质数据库
Uniprot 数据库是收录信息最全面的蛋白质数据库,包含swissport, uniparc, TrEMBL 3个子数据库: 其中swiss-prot 是手工核对过的 ,非冗余, 有详细注释信息的蛋 ...
- 使用PHP生成和获取XML格式数据
1.php生成xml
- IPV6设置
C:\Windows\System32\drivers\etc 目录下修改hosts文件. 网上有更新的ipv6 hosts文件,复制下来~ 别人不断更新的: https://raw.githubus ...
- HTTP 错误 404.3 解决
问题 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决 在开始菜单中找到 Visual Studio 命令提示工具.然后用管理员 ...
- CentOS7.1 Liberty云平台之Dashboard篇(7)
控制节点: 一.安装及配置Dashboard 1.安装dashboard相关包 yum install openstack-dashboard 2.配置/etc/openstack-dashboard ...
- 超全面的JavaWeb笔记day03<JS对象&函数>
1.js的String对象(****) 2.js的Array对象 (****) 3.js的Date对象 (****) 获取当前的月 0-11,想要得到准确的月 +1 获取星期时候,星期日是 0 4.j ...
- POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)
题目 Description One of the more popular activities in San Antonio is to enjoy margaritas in the park ...
- HDOJ 4276 The Ghost Blows Light
题意 1. 给定一棵树, 树上节点有 value, 节点之间 travel 有 cost. 给定起始节点和最大 cost, 求解最大 value 思路 1. 寻找最短路径 a. 题目描述中有两句话, ...