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 ...
随机推荐
- 同时开左右两个SAPGUI编辑器显示同一段ABAP代码
很多文本编辑器都支持同时开左右两个窗口显示同一段代码,使用场景可能是比较同一段代码的不同版本差异,或者是ABAP里,同一段代码在Netweaver不同版本里的实现差异,比如版本为SP1的系统A和版本为 ...
- BZOJ3680:吊打XXX(模拟退火)
Description gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty.gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了.蒟蒻们将 n个gty吊在n根绳子上,每根绳子穿过天台的一个 ...
- Git使用02--branch分支, tag版本, 忽略文件 .gitingore
一.分支 # 查看分支 git branch # 创建分支 git branch 分支名 # 切换分支 git checkout name # 创建并切换分支 git checkout -b name ...
- T-SQL 基础 2
运行结果: while 循环 运行结果: 运行结果 : if 判断语句 计算 1到100的基数 运行结果 1到100的偶数 运行结果 局部变量前缀是 @ 全局变量(包括系统变量) 前缀是 @@
- NTU Long-Term Positioning Dataset
NTU Long-Term Positioning Dataset 地址:http://www.clarenceliang.com/dataset/ 場景:NTU 博理館外廣場 描述:超過一個月連續拍 ...
- google为什么将爬虫从python移植到C++
这是好几年前Quora上的一个问题,有点过时,但看了之后感觉不错,就总结了一下 原文链接:http://www.quora.com/Why-did-Google-move-from-Python-to ...
- Python—面向对象04 绑定方法
坚持把梳理的知识都给记下来....... 嗯哼哼 1.绑定方法与非绑定方法 在类内部定义的函数,分为两大类: 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_m ...
- session和cookie的介绍
1.将cookie,session之前,还是先说说http协议 http协议是基于TCP/UDP之上的应用层一个标准 请求,响应的模式.是你必须先请求到一个服务端之后,服务端才会响应到你.他是不会无缘 ...
- web网络攻击解决方案
原文地址:https://www.xingkongbj.com/blog/http/web-attack.html 产生原因 HTTP 不具备安全功能. 在客户端可以篡改请求. 跨站脚本攻击 XSS ...
- BZOJ1030: [JSOI2007]文本生成器(AC自动机)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5984 Solved: 2523[Submit][Status][Discuss] Descripti ...