dfs Gym - 100989L
AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tries to make it correct as quickly as possible!
Given an equation of the form: A1 o A2 o A3 o ... o An = 0, where o is either + or -. Your task is to help AbdelKader find the minimum number of changes to the operators + and -, such that the equation becomes correct.
You are allowed to replace any number of pluses with minuses, and any number of minuses with pluses.
Input
The first line of input contains an integer N (2 ≤ N ≤ 20), 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 108.
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 题目翻译:一串数字改变其的符号,让sum为0,输出最小的改变次数,不存在则输出-1 运用算法DFS ac代码:
#include<iostream>
using namespace std;
int a[25],n,ans;
void dfs(int index,int sum,int c)
{
if (index==n+1){
if (sum==0){
ans=ans<c?ans:c;
}
return ;
}
int j;
for (j=0;j<2;j++){ //只存在 + 或者是 - 两种情况
if (j==0){
if (a[index]<0)
dfs(index+1,sum-a[index],c+1);
else
dfs(index+1,sum+a[index],c);
}
else if (a[index]<0)
dfs(index+1,sum+a[index],c);
else
dfs(index+1,sum-a[index],c+1);
}
return ;
}
int main()
{
//scanf("%d",&n);
char op;
int i,j;
ans=99999999;
cin>>n;
for (i=1;i<=n;i++){
if (i==1)
scanf("%d",&a[i]);
else{
scanf(" %c %d",&op,&a[i]);
if (op=='-')
a[i]=-a[i];
}
}
dfs(2,a[1],0);
if (ans==99999999)
cout<<"-1\n";
else
cout<<ans<<endl;
return 0;
}
dfs Gym - 100989L的更多相关文章
- Gym 100989L (DFS)
AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...
- DFS Gym 100553J Jokewithpermutation
题目传送门 /* 题意:将字符串分割成一个全排列 DFS:搜索主要在一位数和两位数的处理,用d1, d2记录个数,在不饱和的情况下,两种都试一下 DFS还是写不来,难道是在家里懒? */ #inclu ...
- Gym - 100989L
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- Gym 100463D Evil DFS
Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
随机推荐
- zip4j之加压解压
最近看同事搞个文件打包,搞了大半天,还是有问题!嗨~~ 网上明明有现成的,偏偏要自己写! 下面是基于zip4j实现加压解决的简单工具类 package com.learcher.zip; import ...
- pip pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool
设置超时时间: pip --default-timeout=100 install Pillow
- Machine Learing 入门 —— 开门第0篇
一.最近懒了 7月没怎么写博客,倒是一直在学Machine Learning的入门知识,在这里给大家推荐一个不错的自学网站:https://www.coursera.org/ ,Andrew Ng是联 ...
- 第3次Scrum冲刺
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- QT隐含共享类 QSharedData QSharedDataPointer
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/davidsu33/article/details/28857391 QT中非常多类都实现了隐含共享比 ...
- 动态规划(DP),最大矩阵和
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=74 http://poj.org/problem?id=1050 解题 ...
- 【[AHOI2012]树屋阶梯】
卡特兰数! 至于为什么是卡特兰数,就稍微说那么一两句吧 对于一个高度为\(i\)的阶梯,我们可以在左上角填一个高度为\(k\)的阶梯,右下角填一个高度为\(i-1-k\)的阶梯剩下的我们用一个大的长方 ...
- react中 props,state与render函数的关系
我们很明显的能够感受到,react是一门数据驱动的框架,当数据发生变化,页面就会自动发生变化,他背后的原理是怎么样子的呢 比如todolist例子里面,inputValue变了,框里面的内容就会自动变 ...
- 使用vue搭建项目(创建手脚架)
第一步:切换到创建的目录 创建项目 vue cerate [model] 第二步:切换到创建好的项目,然后创建element vue add element 第三步:创建router vue add ...
- com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭
com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭 解决方案: DBUtil公共方法如下: package ...