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 ;
}
最新文章
- Java程序设计之消费者和生产者
- Python自动化测试 (九)urllib2 发送HTTP Request
- CAD二次开发
- 【JAVA线程间通信技术】
- 生日蛋糕—dfs
- Xcode工程使用CocoaPods管理第三方库新建工程时出现错误
- Easyui使用记录
- Python入门(三,初级)
- 简单Spinner
- Notepad++去除代码行号的几种方法
- linux上备份Oracle时EXP-00091的错误解决方法
- linux命令学习笔记
- nodejs在服务器上运行
- VR问题无关方向,VR全景为您领航,全景智慧城市已势不可当
- Android 类似duplicate entry: android/support/v4/internal/view/SupportSubMenu.class问题解决办法汇总
- java里程碑之泛型--类型通配符
- Django 项目中添加静态文件夹
- bootstrap-typeahead 自动补全简单的使用教程
- python 微信跳一跳辅助 复现
- HNOI 2017
热门文章
- ivew 封装删除 对话框
- Jmeter性能测试结果分析:响应时间为什么是下降的趋势?
- LOJ#2330 榕树之心 树形dp
- Tymeleaf模板引擎背景图片路径书写方式
- 3分钟教会你把封装的js公共方法挂载在vue实例原型上
- Serverless Kubernetes入门:对kubernetes做减法
- java——逻辑运算符与(&;和&;&;)或(|和||)
- 状态管理工具对比vuex、redux、flux
- 贪心整理&;一本通1431:钓鱼题解
- linux 多进程并发服务__关于子进程回收的方法