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

hdu 4277 USACO ORZ (暴力+set容器判重)的更多相关文章

  1. HDU 4277 USACO ORZ(暴力+双向枚举)

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...

  3. hdu 4277 USACO ORZ dfs+hash

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  4. hdu 4277 USACO ORZ DFS

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. hdu 4277 USACO ORZ

    没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  6. hdu 4277 USACO ORZ(dfs+剪枝)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  7. hdu 4277 USACO ORZ (dfs暴搜+hash)

    题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...

  8. hdu 4277 USACO ORZ (Dfs)

    题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...

  9. hdu 5012 bfs --- 慎用STL 比方MAP判重

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 发现一个问题 假设Sting s = '1'+'2'+'3'; s!="123"!!! ...

随机推荐

  1. 可选头 IMAGE_OPTIONAL_HEADER

    //IMAGE_OPTIONAL_HEADER结构(可选映像头) typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // W ...

  2. IOS NSUserDefaults 讲解 用法

    IOS NSUserDefaults 讲解 用法    NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults ...

  3. 方便john破解linux密码批处理

    Title:方便john破解linux密码批处理 -- 2011-11-23 17:31 自定义文件HASH名和字典名批处理(单一文件): @echo offcolor 0asetlocal enab ...

  4. clone database and rename

    使用 management studio right click database -> Tasks -> Generate Scripts -> next until " ...

  5. 一句话输出NGINX日志访问IP前十位排行

    AWK的数组字段自增加,然后取值的方法,要记得哟. 还有,SORT指定列排行,也常用的.

  6. 【HDOJ】4972 A simple dynamic programming problem

    水题. #include <cstdio> #include <cstring> #include <cstdlib> int abs(int x) { ? -x: ...

  7. BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved:  ...

  8. 2015第24周五Spring的AOP

    AOP(面向方面编程:Aspect Oriented Programing)和IoC一样是Spring容器的内核,声明式事务的功能在此基础上开花结果.但AOP的应用场合是受限的,它一般只适合于那些具有 ...

  9. 2014-08-13 SQL语句之Left Join

    今天是在吾索实习的第26天.这天在处理数据库数据的时候发现了一个不错的语句就是Left Join,即左连接.  其功能是:即使右表中没有匹配,也从左表返回所有的行.也就是说,显示的行数与左表一致,且当 ...

  10. HDOJ(HDU) 1407 测试你是否和LTC水平一样高(暴力)

    Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数 ...