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坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
随机推荐
- Setting up a Hadoop cluster - Part 1: Manual Installation
http://gbif.blogspot.com/2011/01/setting-up-hadoop-cluster-part-1-manual.html
- 4.1.4 Nim
Problem description: 有n堆石子,每堆各有ai颗石子.A和B轮流从非空的石子堆中取走至少一颗石子.A先取,取光所有石子的一方获胜.当双方都采用最佳策略时,谁会获胜? 1<=n ...
- python:字典嵌套列表
Python的字典{ }以键值对的形式保存数据,可以以键来访问字典中保存的值而不能用下标访问.字典中几乎可以包含任意的变量,字典,数列,元组.数列也一样. python的列表[ ]与字典不同,列表通过 ...
- dp 最大正方形
洛谷P1387 最大正方形 分析:画图模拟,发现以坐标(i,j)为右下顶点的正方形可以根据它左上方的三个点得到,就很轻松写出状态转移方程了. 代码: #include<bits/stdc++.h ...
- [Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.
待解决 [Fiddler] ReadResponse() failed: The server did not return a complete response for this request. ...
- python 日期操作【转】
SQLite中的时间日期函数这是我学习SQLite时做的笔记,参考并翻译了Chris Newman写的<SQLite>中的<Working with Dates and Times& ...
- 使用ajax请求接口,跨域后cookie无法设置,全局配置ajax;及使用axios跨域后cookie无法设置,全局配置axios
问题一: 使用ajax/axios跨域请求接口,后端放行了,能够正常获取数据,但是cookie设置不进去,后端登录session判断失效 ajax解决办法: //设置ajax属性 crossDomai ...
- maven中 install的install:install的区别
如果一个项目,你想安装jar包到本地仓库,可能会报The packaging for this project did not assign a file to the build artifact ...
- Hadoop 2.7.3 完全分布式维护-动态增加datanode篇
原有环境 http://www.cnblogs.com/ilifeilong/p/7406944.html IP host JDK linux hadop role 172.16.101 ...
- MongoDB文档的基本操作
1. MongoDB的安装方法 (1)下载MongoDB 相应的版本: (2)设置数据文件和日志文件的存放目录: (3)启动MongoDB服务: (4)将MongoDB作为服务启动. 2. Mongo ...