Gym - 100989M(dp)
George met AbdelKader in the corridor of the CS department busy trying to fix a group of incorrect equations. Seeing how fast he is, George decided to challenge AbdelKader with a very large incorrect equation. AbdelKader happily accepted the challenge!
Input
The first line of input contains an integer N (2 ≤ N ≤ 300), the number of terms in the equation.
The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 300.
Values and operators are separated by a single space.
Output
If it is impossible to make the equation correct by replacing operators, print - 1, otherwise print the minimum number of needed changes.
Examples
7
1 + 1 - 4 - 4 - 4 - 2 - 2
3
3
5 + 3 - 7
-1
题意:给你一个表达式(只包括数字,符号(“+”或“-”)),问你是否能改变最少的符号使得表达式值为0.
思路:看到这题,一开始就用dfs做,但是因为数据量太大没过,后来听学长讲才知道这道题可以用背包做。。。。
背包选择范围(0---sum),我们以sum/2+s[1]为起点,sum/2为终点,因为当我们已经选择变化的和不能超过sum/2(因为绝对值的和才sum,如果你选的超过了一半,那么无论后面怎么选择总和都不可能为0);
代码:
#include<stdio.h>
#define INF 0x3fffffff
#include<string.h>
int dp[2][90000];
int s[305];
int min(int a,int b){
if(a<b)
return a;
return b;
}
int main(){
int n;
scanf("%d",&n);
int i,j;
int sum=0;
scanf("%d",&s[1]);
sum=s[1]; char b;
for(i=2;i<=n;i++){
getchar();
scanf("%c %d",&b,&s[i]);
sum=sum+s[i];
if(b=='-')
s[i]=-s[i];
}
if(sum%2==1)
printf("-1\n");
else{
int a,b;
a=1;
memset(dp[!a],0x3f,sizeof(dp[!a]));
dp[0][sum/2+s[1]]=0;//如果选择的总数和超过了sum/2,它就回不来了
for(i=2;i<=n;i++){
memset(dp[a],0x3f,sizeof(dp[a]));
for(j=0;j<=sum;j++){
if(j-s[i]>=0)
dp[a][j]=min(dp[a][j],dp[!a][j-s[i]]);
if(j+s[i]>=0)
dp[a][j]=min(dp[a][j],dp[!a][j+s[i]]+1);
}
a=!a;
}
if(dp[!a][sum/2]>sum/2)
printf("-1\n");
else
printf("%d\n",dp[!a][sum/2]);
}
return 0;
}
Gym - 100989M(dp)的更多相关文章
- GYM 101933A(dp)
要点 \(\sum{w_i} <= 1e8\)是有意味的. 设\(dp[i]\)为至少可以承受重量\(i\)的最大可达高度.转移时可以转移的\(j\)必须满足加上它之后得保证各层不能超重,所以\ ...
- GYM 101889E(dp)
dp[i][j][k]表示第i位填数字k时,与后面的相连模数为j时,后面的数字最小填多少. 测得我提心吊胆还以为复杂度高了,结果出来46ms还是cf评测姬强啊. #pragma comment(lin ...
- GYM 101673G(dp)
dp[i][j][0/1]:第i天处于第j状态要不要吃. const int maxn = 1e2 + 5; int n, a[maxn], b[maxn]; int dp[maxn][maxn][2 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
随机推荐
- mongodb shell和Node.js driver使用基础
开始: Mongo Shell 安装后,输入mongo进入控制台: //所有帮助 > help //数据库的方法 > db.help() > db.stats() //当前数据库的状 ...
- uva10780
将m分解质因数,然后计算次数取最小. #include <iostream> #include <cstdio> #include <cmath> #include ...
- 字符串hash
hash[i]=(hash[i-1]*p+idx(s[i]))%mod p和mod取不同的较大的素数
- leetcode-algorithms-19 Remove Nth Node From End of List
leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...
- OpenStack设计与实现5——RESTful API和WSGI
转https://segmentfault.com/a/1190000004361778 Tips:文章为拜读@xingjiarong 后有感而做的分享,先对作者表示感谢,附原文地址:http://b ...
- Mybatis自动生成实体类,映射文件,dao
http://www.mybatis.org/generator/index.html 方法一:eclipse插件式 1.下载 mybatis-generator-core-1.3.2.jar 解压后 ...
- Nodejs--util模块
util.inspect util.inspect是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出. 它至少接受一个参数 object,即要转换的对象. util.inspect(obje ...
- Hadoop---Java-API对HDFS的操作
Java-API对HDFS的操作 哈哈哈哈,深夜来一波干货哦!!! Java-PAI对hdfs的操作,首先我们建一个maven项目,我主要说,我们可以通过Java代码来对HDFS的具体信息的打印,然后 ...
- Farm Irrigation(非常有意思的并查集)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- AMR文件结构
转自:http://blog.csdn.net/dinggo/article/details/1966444 https://blog.csdn.net/wlsfling/article/detail ...