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 ...
随机推荐
- pthread使用
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/CreatingTh ...
- Android4.4 ContentResolver查询图片无效 及 图库删除 添加图片后,ContentResolver不更新的问题解决
问题背景: 參考链接 做了一个图片浏览,用ContentResolver扫描图库照片.且严格依照时间拍摄顺序排好序显示在listview里.例如以下图所看到的: watermark/2/text/aH ...
- HDU 4278 卡特兰,区间DP
题意:每个人有一个DI值,现在有一个小黑屋,这些人的顺序可以利用这个小黑屋调整,调整方式是入栈出栈方式,也就是说,这里的方案是有卡特兰数个方式. 调整后使得 d1*0 + d2*1 + d3*2 + ...
- idea 使用maven构建项目时,target bytecode version经常自动变化
解决方法:在工程的pom.xml中添加 <build> <plugins> <plugin> <groupId>org.apache.maven.plu ...
- c#运用this.invoke() 在多线程时对UI进行修改
什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的,线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈 ...
- eclipce导出项目发布到tomcat
1.右击项目-Except 2.在弹出框中输入“WAR file” 3.点击“next” 在Destinatin选择保存路径,即可 4.将保存的文件复制到tomcat下,启动tomcat之后,会自动解 ...
- [LuoguP1141]01迷宫
1141 01迷宫 题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务 ...
- javascript之Window 对象
一.说明:他是JS中最大的对象,它描述的是一个浏览器窗口,一般要引用他的属性和方法时,不需要用“Window.XXX”这种形式,而是直接使用“XXX”.一个框架页面也是一个窗口. 二.Window窗口 ...
- 复合词(Compound Words, UVa 10391)(stl set)
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...
- redis的数据结构与命令
以下部分文档,摘自51cto讲师:汤小洋 redis提供五种数据类型:string,hash,list,set及zset(sorted set). Redis数据就是以key value形式来存储的 ...