4277 USACO ORZ

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Puppettt/article/details/76984609

USACO ORZ

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


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



dfs 把篱笆分成三段 围成三角形 存在多少不同的三角形;

AC:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <algorithm>
typedef long long LL;
using namespace std;
int l[20]={0},s[3],n;
set< pair<int,int> >ans;
void dfs(int);
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        memset(s,0,sizeof(s));
        ans.clear();
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%d",&l[i]);
        dfs(0);

        printf("%d\n",ans.size());
    }
}
void dfs(int deep)
{
    if(deep==n){
        if(s[0]<=s[1]&&s[1]<=s[2]&&s[0]+s[1]>s[2])
            ans.insert(make_pair(s[0],s[1]));
        return;
    }
    for(int i=0;i<3;i++)
    {
        s[i]+=l[deep];
        dfs(deep+1);
        s[i]-=l[deep];
    }
}



 

猜你喜欢

转载自blog.csdn.net/Puppettt/article/details/76984609