hdu4277USACO ORZ dfs暴力枚举+map

//给N个 问rails用着N个rails能构成几个不同的三角形
//dfs暴力枚举+(map)
#include<cstdio>
#include<cstring>
#include<map>
#include<iostream>
using namespace std ;
int ans ;
int a[20] ;
int n ;
int sum  =  0 ;
struct node
{
    int a , b , c ;
    bool operator == (const node d)const
    {return (d.a==a&&d.b==b&&d.c==c);}
};
bool operator < (const node d , const node e){
    if(d.a == e.a)
    {
        if(d.b == e.b)
        return d.c < e.c ;
        return d.b < e.b ;
    }
    return d.a < e.a;}
bool operator == (const node d , const node e){return (d.a==e.a&&d.b==e.b&&d.c==e.c);}
map<node , int> ma ;
void dfs(int sum_1 , int sum_2 , int sum_3 ,int pos)
{
    if(sum_1 > sum/2 || sum_2 > sum/2 || sum_3 > sum/2)
    return ;
    if(pos == n + 1)
    {
        if(sum_1 <= sum_2 && sum_2 <= sum_3)
        if(sum_2 + sum_3 > sum_1 && sum_3)
        {
            struct node  a = {sum_1 , sum_2 , sum_3} ;
            if(ma[a] == 0)
            {
                ans++ ;
                ma[a] = 1;
            }
        }
        return ;
    }
    dfs(sum_1+a[pos] , sum_2 , sum_3 , pos+1) ;
    dfs(sum_1 , sum_2 + a[pos] , sum_3 , pos+ 1) ;
    dfs(sum_1 , sum_2 , sum_3+ a[pos] , pos+1) ;
}
int main()
{
   //freopen("in.txt" ,"r" , stdin) ;
    int T ;
    scanf("%d" ,&T) ;
    while(T--)
    {
        sum =  0 ;
        scanf("%d" ,&n) ;
        for(int i = 1;i <= n;i++)
        scanf("%d" ,&a[i]) ,sum += a[i] ;
        ans = 0 ;ma.clear() ;
        dfs(0 , 0 , 0 ,1) ;
        printf("%d\n" ,ans) ;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 17:26:30

hdu4277USACO ORZ dfs暴力枚举+map的相关文章

ZOJ 3816 Generalized Palindromic Number dfs+暴力枚举

题目链接:点击打开链接 题意: 给定一个数n 找一个最大的数u使得u<n && u为回文. 枚举前面有多少位是一样的.然后分类讨论.啪啦啪啦 #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <vector> using namespace std; typedef long long ll; cons

Network Saboteur (DFS暴力枚举)

A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts. A disgruntled computer scienc

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

HDU 4277 USACO ORZ(暴力+双向枚举)

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3809    Accepted Submission(s): 1264 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastu

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

Coderforces 633D:Fibonacci-ish(map+暴力枚举)

http://codeforces.com/problemset/problem/633/D D. Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f0 and f1 are arbi

[ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number. We call a number 

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树+暴力枚举边)

题目大意:给你1000个点,每个点上有一个数目代表这个城市有多少人,让你把这N个点构成一颗生成树,你可以删除其中的任意一条边.让你求出一个比例A/B是的这个比例最大,A表示你删除那条边上两个城市的人口数之和,B表示的是去掉这条变这可生成树上其他的边的总长度. 解体思路:先求出来最小生成树,然后暴力枚举生成树的边,B=总数-这条边的长度.A = 将这条连断开之后左右集合中权值最大的两个数的和. 这样保证了B最小的情况下,去找最大的A,所以是可行的解.生成树的同时建边,然后dfs找最大值. PS:这