C-USACO ORZ

来源:互联网 发布:飞机票哪个软件便宜 编辑:程序博客网 时间:2024/06/05 10:28

USACO ORZ

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

132 3 4

Sample Output

1

AC代码讲解:

#include<iostream>#include<cstdio>#include<algorithm>#include<set>using namespace std;set <long long>s;int a,b,c;int l[1000010],n;void dfs(int i){    if(i==n+1)    {        if(a&&a<=b&&b<=c&&a+b>c)        {            long long z=a*10000000+b;            //这是一种非常好的降低复杂度的做法,将二维的比较降到一维上;            //还可以set<pair<int a,int b>>s;这是我从陈学弟那看到的操作;            s.insert(z);        }        return ;    }    a+=l[i];dfs(i+1);a-=l[i];    b+=l[i];dfs(i+1);b-=l[i];    c+=l[i];dfs(i+1);c-=l[i];}int main(){   int T;   scanf("%d",&T);   while(T--)   {       scanf("%d",&n);       for(int i=1;i<=n;i++)       {           scanf("%d",&l[i]);       }       a=0;b=0;c=0;       s.clear();       dfs(1);       printf("%d\n",s.size());   }    return 0;}

上面的降维比较是通过hash表得到的启发,f(x,y)=x*max(y)+y;就可以的得到一个f(x,y)与x,y的一一映射关系。
还有相关的DFS,学妹问了我几个很好的问题,是否都要回溯,我个人认为是只要需要进行试探就必须要回溯,因为人总有探索失败的时候。返回上一次换一条路走。树要一层的每一个分支都要访问完。(当然要剪支就剪支)明天再做几个DFS巩固一下。^-^

原创粉丝点击