HDOJ 4277 USACO ORZ 搜索+剪枝

来源:互联网 发布:剑雨江湖仙器进阶数据 编辑:程序博客网 时间:2024/06/05 06:56

暴力搜索+轻微剪枝
固定第一个数的话可以减少2/3的时间…..

USACO ORZ

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

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
2012 ACM/ICPC Asia Regional Changchun Online

/* ***********************************************Author        :CKbossCreated Time  :2015年09月07日 星期一 21时39分52秒File Name     :HDOJ4277.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;int n,sum;int a[20];int seg[3];int tmp[3];set<int> st;void check(){    for(int i=0;i<3;i++) tmp[i]=seg[i];    sort(tmp,tmp+3);    if(tmp[0]+tmp[1]>tmp[2])         st.insert(tmp[0]*160000+tmp[1]);    //if(st.count(tmp[0]*300000+tmp[1])==0) cout<<tmp[0]<<","<<tmp[1]<<","<<tmp[2]<<endl;}void dfs(int x){    if(x==n)    {        check();        return ;    }    for(int i=0;i<3;i++)    {        seg[i]+=a[x];        if(seg[i]*2<sum)             dfs(x+1);        seg[i]-=a[x];    }}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T_T;    scanf("%d",&T_T);    while(T_T--)    {        scanf("%d",&n);        sum=0;        for(int i=0;i<n;i++)         {            scanf("%d",a+i);            sum+=a[i];        }        sort(a,a+n);        memset(seg,0,sizeof(seg));        st.clear();        seg[0]=a[0];        dfs(1);        cout<<st.size()<<endl;    }    return 0;}
0 0
原创粉丝点击