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坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
随机推荐
- InputSream转为String
public static String convertStreamToString(InputStream is) { /* * To convert the InputStream to Stri ...
- 进程锁,队列,JoinableQueue
内容梗概: 1.进程同步(锁) 2.队列(重点) 3.生产者消费者模式 4.JoinableQueue([maxsize]) 5.信号量(了解) 6.事件 1.进程同步(锁) 并发编程让我们能更加充分 ...
- css 选择器三
2.4.10 浮动 浮动是css里面布局最多的一个属性,也是很重要的一个属性. float:表示浮动的意思.它有四个值. none: 表示不浮动,默认 left: 表示左浮动 right:表示右浮动 ...
- springboot使用redis的配置application.yml连接池以及存取
第一步:添加pom文件依赖: <!-- redis --> <dependency> <groupId>redis.clients</groupId> ...
- java压缩流
java压缩流是为了减少传输时的数据量,可以将文件压缩成ZIP.JAR.GZIP等文件格式.
- docker实战系列之docker 端口映射错误解决方法
错误: Error response from daemon: Cannot start container web: iptables failed: iptables -t nat -A DOCK ...
- [CodeForces - 614B] B - Gena's Code
B - Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There ...
- Oracle物理DG自动切换——Dataguard Broker配置
1.前言部分 1.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能: ① Data Guard Broker 的配置 ② Fast-Start Failover 的配置 ③ Oracle D ...
- nginx 隐藏 index.php 和 开启 pathinfo 模式的配置
nginx 通过 location 的规则匹配将 php 转发给 php-fpm 处理后获取结果然后返回给客户端,转发模式可以通过 unix sock 或 tcp socket 方式.百度了好多文章我 ...
- 使用Spring-data-jpa(2)(三十一)
创建实体 创建一个User实体,包含id(主键).name(姓名).age(年龄)属性,通过ORM框架其会被映射到数据库表中,由于配置了hibernate.hbm2ddl.auto,在应用启动的时候框 ...