zoukankan      html  css  js  c++  java
  • dfs(枚举)

    http://codeforces.com/gym/100989/problem/L

    L. Plus or Minus (A)
    time limit per test
    1.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    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.

    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
    Input
    Copy
    7
    1 + 1 - 4 - 4 - 4 - 2 - 2
    Output
    Copy
    3
    Input
    Copy
    3
    5 + 3 - 7
    Output
    Copy
    -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[29];
    char c[29];
    int n ;
    int min1 = INF ;
    int vis[29];
    
    void dfs(int s , int l , int ans)
    {
        //cout << s  << " " << l << " " << ans << endl ;
        if(l == n)
        {
            if(s == 0)
            {
                min1 = min(min1 , ans);
            }
            return ;
        }
        if(c[l] == '-')
        {
            dfs(s+a[l] , l+1 , ans + 1);
            dfs(s-a[l] , l+1 , ans);
        }
        if(c[l] == '+')
        {
            dfs(s+a[l] , l+1 , ans);
            dfs(s-a[l] , l+1 , ans+1);
        }
    }
    
    int main()
    {
        scanf("%d" , &n);
        scanf("%d " , &a[0]);
        for(int i = 1 ; i < n ; i++)
        {
            cin >> c[i] >> a[i];
        }
        dfs(a[0] , 1 , 0);
        if(min1 != INF)
            cout << min1 << endl ;
        else
            cout << -1 << endl ;
    
    
        return 0;
    }
  • 相关阅读:
    MVC OnActionExecuting,OnResultExecuted 的用法
    MindManager脑图之项目管理甘特图
    jQuery.Autocomplete实现自动完成功能(经典)
    常见26个jquery使用技巧详解(比如禁止右键点击、隐藏文本框文字等)
    用ATL创建COM组件详细解说
    STL中的常用的vector,map,set,Sort用法
    绘图效率完整解决方案——三种手段提高GDI/GDI+绘图效率
    C++面试
    iOS开发UI篇—iOS开发中三种简单的动画设置
    OS开发UI篇—IOS开发中Xcode的一些使用技巧
  • 原文地址:https://www.cnblogs.com/nonames/p/11584882.html
Copyright © 2011-2022 走看看