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. Python自动化运维之8、正则表达式re模块

    re模块 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串,在文本处理方面功能非常强大,也经常用作爬虫,来爬取特定内容,Python本身不支持正则,但是通过导入re模块,Python ...

  2. 《python基础教程》笔记之 字典

    字典创建 字典由多个键值对组成,每个键和对应值之间用冒号隔开,项之间用逗号隔开,而整个字典用一对大括号括起来,如 >>> phonebook={'alice':'0123', 'Be ...

  3. fabric自动化部署django

    使用fabric部署django应用 使用fabric部署django应用 本文是我的网站易读中文网自动化部署的脚本实现,以下代码在ubuntu和debian中测试通过 由于网站使用的是python技 ...

  4. T-SQL事务实例

    begin try begin tran ,'); ; --RAISERROR ('Error raised in TRY block.',16,1); commit tran end try beg ...

  5. 生产环境下Centos 6.5优化配置 (装载)

    本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡 2.ifconfig查询IP进行SSH链接 3.更新系统源并且升级系统 4.系统时间更新和设定定时任 5. ...

  6. 防止ajax非正常访问

    http://www.cnblogs.com/yagzh2000/archive/2013/06/09/3128042.html http://www.cnblogs.com/henw/archive ...

  7. C# and android and socket

    利用TCP协议通过Socket编写的网络聊天工具1-客户端 利用TCP协议通过Socket编写的网络聊天工具2-通用类设计 利用TCP协议通过Socket编写的网络聊天工具3-服务器端设计

  8. linux 建库,编码,导入数据

    二.导入数据库1.首先建空数据库mysql>create database abc; 2.导入数据库方法一:(1)选择数据库mysql>use abc;(2)设置数据库编码mysql> ...

  9. java 枚举类型

    原来枚举类型还可以这样玩... public enum Tenum { None(1),ByteArray(2),List(3),Map(4); private int id; private Ten ...

  10. Python 2 到 Python 3的变化

    Python 2.x到Python 3.x变化还是挺大的,具体的变化,参考官方文档: https://docs.python.org/3.0/whatsnew/3.0.html