hdu 4277 USACO ORZ (暴力+set容器判重)

Posted on 2013-07-26 19:22  冰天雪域  阅读(245)  评论(0编辑  收藏  举报

USACO ORZ

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

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
 

 

Recommend
liuyiding
 
题意:
给你n条线段,问全部用上能围成多少个不同的三角形。
 
分析:暴力枚举的话,3^15。关键是判重,利用set容器(集合)的特点可以做到。然后就是分别把边加到三边中的哪一边,依次枚举。将三角形封装成一个结构体,重载< 、==
           和+。加边的时候两个set轮换。
 
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
using namespace std;

struct node
{
    int a,b,c;
    node(){a=b=c=0;}
    node(int aa,int bb,int cc):a(aa),b(bb),c(cc){}

    bool operator < (const node &tmp) const
    {
        if(a!=tmp.a) {return a<tmp.a;}
        else if(b!=tmp.b) {return b<tmp.b;}
        else {return c<tmp.c;}
    }

    bool operator == (const node &tmp) const
    {
        return (a==tmp.a && b==tmp.b && c==tmp.c);
    }
    node operator + (const node &tmp) const
    {
        int aa=a+tmp.a,bb=b+tmp.b,cc=c+tmp.c;
        if(aa>cc) {swap(aa,cc);}
        if(bb>cc) {swap(bb,cc);}
        if(aa>bb) {swap(aa,bb);}
        return node(aa,bb,cc);
    }
};

int isok(const node &tmp)
{
    return (tmp.a+tmp.b>tmp.c);
}

set<node> s[2];
vector<int> myv;

int main()
{
    int x,t,n,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        myv.clear();
        for(i=0;i<n;i++)
        {
            scanf("%d",&x);
            myv.push_back(x);
        }
        s[0].clear();
        s[0].insert(node());

        int a=0,b=1;
        set<node>::iterator it;
        for(i=0;i<n;i++)
        {
            s[b].clear();
            for(it=s[a].begin();it!=s[a].end();++it)
            {
                s[b].insert(*it+node(myv[i],0,0));
                s[b].insert(*it+node(0,myv[i],0));
                s[b].insert(*it+node(0,0,myv[i]));
            }
            swap(a,b);
        }
        int ans=0;
        for(it=s[a].begin();it!=s[a].end();++it)
        {
            if(isok(*it)) ++ans;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

Copyright © 2024 冰天雪域
Powered by .NET 8.0 on Kubernetes