dfs(枚举)
http://codeforces.com/gym/100989/problem/L
1.0 s
256 MB
standard input
standard output
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.
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.
If it is impossible to make the equation correct by replacing operators, print - 1, otherwise print the minimum number of needed changes.
7
1 + 1 - 4 - 4 - 4 - 2 - 2
3
3
5 + 3 - 7
-1
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <ctype.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 10
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[];
char c[];
int n ;
int min1 = INF ;
int vis[]; void dfs(int s , int l , int ans)
{
//cout << s << " " << l << " " << ans << endl ;
if(l == n)
{
if(s == )
{
min1 = min(min1 , ans);
}
return ;
}
if(c[l] == '-')
{
dfs(s+a[l] , l+ , ans + );
dfs(s-a[l] , l+ , ans);
}
if(c[l] == '+')
{
dfs(s+a[l] , l+ , ans);
dfs(s-a[l] , l+ , ans+);
}
} int main()
{
scanf("%d" , &n);
scanf("%d " , &a[]);
for(int i = ; i < n ; i++)
{
cin >> c[i] >> a[i];
}
dfs(a[] , , );
if(min1 != INF)
cout << min1 << endl ;
else
cout << - << endl ; return ;
}
dfs(枚举)的更多相关文章
- POJ1288 Sly Number(高斯消元 dfs枚举)
由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- The Pilots Brothers' refrigerator DFS+枚举
Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
随机推荐
- MASM DEBUG LINKER免费下载
这资源全被CSDN霸占了,对于我这种不使用CSND的人,没积分,真TM不好找,搞个共享的. 网盘链接,永久有效 https://pan.baidu.com/s/1Ws5axrfos1cpWL9jyAE ...
- pymongo操作mongo数据库的查操作
一: 数据结构 { "_id" : ObjectId("5de8a5b748a75a8d48b72bdc"), ", ", ", ...
- MongoDB的$作为下标的用法
在MongoDB中有一个非常神奇的符号 "$" "$" 在 update 中 加上关键字 就 变成了 修改器 其实 "$" 字符 独立出现 ...
- java:Conllection中的List,ArrayList添加元素,删除元素,输出元素
java:Conllection中的List,ArrayList添加元素,删除元素,输出元素 //为list接口实例化 List<String> addlist = new ArrayLi ...
- 【leetcode】1143. Longest Common Subsequence
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...
- echart--如何将echart的配置项,放到webpack中(CHARTTEMPLATE时)
1.假如,我们已经写好了组件,我们需要把它放入到一个环境中去 2.首先在index.html中,我们需要写一个dom结构 3.新建一个,chart.js文件(这个里面放组件的代码) 1>开始创建 ...
- HTML中表格table标签的实例
一.表格有边框,第一行居中对齐 二.表格没有边框 三.表格有水平标题 四.表格有垂直标题 五.合并行单元格 colspan合并单元格 六.表格有单元格边距(内边距) 七.表格没有单元格间距 八.表格有 ...
- Alexa TOP 100万的域名列表
Alexa是一家专门发布网站世界排名的网站,是亚马逊公司的一家子公司.Alexa每天在网上搜集多达几十亿的网址链接,而且为其中的每一个网站进行了排名. Alexa通过Alexa官网查询好像TOP 50 ...
- Java 实现 对象和转字符串之间的互转 (json格式)
添加依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> ...
- iview 父组件动态传值给子组件
父组件 <maintenance-super :show="{'modalSuper':modalSuper,'myData':myData}" @on-close=&quo ...