hdu 4277 USACO ORZ

USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4265    Accepted Submission(s): 1409


Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments. 
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.
 

Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)
 

Output
For each test case, output one integer indicating the number of different pastures.
 

Sample Input
1 3 2 3 4
 

Sample Output
1
 

Source
 
 
题意:给你n块木板,从中组合成三块,木板全部用上; 这三块能形成一个三角形,问有多少种三角形可以形成;不同的三角形至少有一条边不同;.
思路:暴力dfs
 
/**
 
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<iomanip>
using namespace std;
typedef pair<int,int> pr;
const int maxn = 16;
int n, a[maxn], ans, s, b[3], t;
map<pr,int>mp;
void dfs(int suma,int sumb,int sumc,int i)
{
//    if(suma>sumb||suma>sumc||sumb>sumc) return 0;
    if(suma>s/2||sumb>s/2||sumc>s/2) return ;//构成三角形满足条件;
    if(i==n){
        b[0] = suma, b[1] = sumb, b[2] = sumc;//数组b在里面定义会超时;
        if(b[0]>b[1]){
            t = b[0], b[0] = b[1], b[1] = t;
        }
        if(b[0]>b[2]){
            t = b[0], b[0] = b[2], b[2] = t;
        }
        if(b[1]>b[2]){
            t = b[1], b[1] = b[2], b[2] = t;
        }
        if(b[0]==0) return ;
        pr pp(b[1],b[2]);
        if(!mp[pp]&&b[0]+b[1]>b[2]){
            mp[pp] = 1; ans++;
        }
        return ;
    }
    dfs(suma,sumb,sumc+a[i],i+1);
    dfs(suma,sumb+a[i],sumc,i+1);
    dfs(suma+a[i],sumb,sumc,i+1);
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        mp.clear();
        ans = 0;
        s = 0;
        for(int i = 0; i < n; i++) scanf("%d",&a[i]), s+=a[i];
   //     sort(a,a+n);
        dfs(a[0],0,0,1);//第一个放哪里都一样,根据对称性;没他会超时;
        printf("%d ",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiaochaoqun/p/4992093.html