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

本文讨论如何使用给定的线段构建不同的三角形,并详细解释了实现这一目标的方法,包括关键步骤、判重技巧和代码实现。

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值