UESTC 2016 Summer Training #1 Div.2 L - Plus or Minus (A) dfs

L - Plus or Minus (A)

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice Gym
100989L

Description

standard input/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: A1oA2oA3o ... oAn?=?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.

Sample Input

Input

7
1 + 1 - 4 - 4 - 4 - 2 - 2

Output

3

Input

3
5 + 3 - 7

Output

-1

Source

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121539#problem/L

My Solution

dfs就好, 好久没用写dfs了,简单dfs还是Debug了好长时间, 尴尬⊙﹏⊙‖∣

记得把那些转移的东西写在参数里

读入char类型, 记得看看要不要用getchar吸掉换行空格什么的

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 28;
int val[maxn];
char plusmi[maxn];
int ans, n;
void dfs(int k, int sum, int q)
{
    if(k == n){
        if(sum == 0) ans = min(ans, q);
        return;
    }

    for(int i = 0; i < 2; i++){
        if(i == 0){
            if(plusmi[k] == '-') dfs(k+1, sum + val[k], q + 1);
            else dfs(k+1, sum + val[k], q);
        }
        else{
            if(plusmi[k] == '+') dfs(k+1, sum - val[k], q + 1);
            else dfs(k+1, sum - val[k], q);
        }
    }
}

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    //freopen("b.txt", "w", stdout);
    int T = 2;
    while(T--){
    #endif // LOCAL
    scanf("%d", &n);
    scanf("%d", &val[0]);
    for(int i = 1; i < n; i++){

        getchar();
        scanf("%c", &plusmi[i]);
        scanf("%d", &val[i]);

    }
    /*
    printf("%d", val[0]);
    for(int i = 1; i < n; i++)
        printf("%c%d",plusmi[i] , val[i]);
    */
    ans = 1000;
    dfs(1,val[0], 0);

    if(ans != 1000) printf("%d", ans);
    else printf("-1");

    #ifdef LOCAL
    printf("\n");
    }
    #endif // LOCAL
    return 0;
}

Thank you!

------from ProLights

时间: 2024-09-30 20:40:37

UESTC 2016 Summer Training #1 Div.2 L - Plus or Minus (A) dfs的相关文章

UESTC 2016 Summer Training #5 Div.2(未完待续)

A #include <cstdio> #include <cstring> #include <vector> #define MAXN 100005 #define mem(a) memset(a, 0, sizeof(a)) using namespace std; int TreeArray[MAXN], Left[MAXN], Right[MAXN], Fork[MAXN]; typedef vector<int> Ve; vector<Ve

UESTC 2016 Summer Training #1 Div.2

最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria (B) 不会捉 Gym 100989E 水 Gym 100989F 水 Mission in Amman (B) 没看题 Queue (A) 感觉题意理解得有问题啊 1 #include <iostream> 2 #include <cstdio> 3 #include <cstr

UESTC 2016 Summer Training #2 Div.2 A dp、递推、多阶段问题

A - A Time Limit:336MS     Memory Limit:1572864KB     64bit IO Format:%lld & %llu Submit Status Practice SPOJ AMR11A Description Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did we not tell you that

UESTC 2016 Summer Training #1 Div.2 E - Accepted Passwords 讨论

E - Accepted Passwords Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice Gym 100989E Description standard input/output Islam is usually in a hurry. He often types his passwords incorrectly. He hates

UESTC 2016 Summer Training #1 Div.2 F - Mission in Amman (A) 动态维护(刷新:--、++)

F - Mission in Amman (A) Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice Gym 100989F Description standard input/output You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the che

UESTC 2016 Summer Training #1 Div.2 H - Queue (A) 贪心

H - Queue (A) Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice Gym 100989H Description standard input/output After the data structures exam, students lined up in the cafeteria to have a drink and ch

UESTC 2016 Summer Training #2 Div.2 E 分解质因素(除了以后剩下的可能也是个素数)

E - E Time Limit:3000MS     Memory Limit:1572864KB     64bit IO Format:%lld & %llu Submit Status Practice SPOJ AMR11E Description Arithmancy is Draco Malfoy's favorite subject, but what spoils it for him is that Hermione Granger is in his class, and

UESTC 2014 Summer Training #18 Div.2

A.UVALive 6661 题意从1~N中选k个数,和为s的方案数 第一眼搜索,估计错状态量,又去yydp...浪费大量时间 数据很小的,状态数都不会超过2^N...直接dfs就过了 //state二进制表示选取的数 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn = 200; in

UESTC 2014 Summer Training #3 Div.2

(更新中) A:ZOJ 3611 BFS+状态压缩 [题意]:给定一张n*m的图,图上每个点有如下情况:L,R,D,U:代表在该点上只能往它已经给定的方向前进.#,W:不能走到该点.$:走到该点,可以花两分钟得到一分值,然后可以从该点向任意方向走.0:走到该点后可以向任意方向走.然后给你起点和终点坐标,问是否能从起点走到终点,如果能,求出可获得的最大分值以及与之对应达到该最大分值所需的的最小时间花 费.(其中起点和终点坐标可以相同)[知识点]:BFS+状态压缩[题解]:我觉得超级棒的题!真心感觉