USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3809    Accepted Submission(s): 1264

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
 

题意:给你n条边。组成一个三角形。求能组成的三角形种数。两个三角形三条边一样的算一种。

题解:暴力枚举第一条边。然后双向枚举第二条边,用map去重。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef pair<int,int > ppi;
map<ppi,int> mp;
int a[20];
int b[20];
int sum;
int all;
int bn;
int ans;
int smallest;
int largest;
void dfs(int i,int csum) {
if(csum>smallest)
return ;
if(csum!=0&&all-csum<=largest&&mp.find(MP(csum,all-csum))==mp.end()) {///成立&&判重
ans++;
mp[MP(csum,all-csum)]=1;
}
if(i==bn)
return ;
dfs(i+1,csum+b[i]);
dfs(i+1,csum);
}
int main() {
int n;
int t;
scanf("%d",&t);
while(t--) {
mp.clear();
scanf("%d",&n);
sum=0;
ans=0;
for(int i=0; i<n; i++) {
scanf("%d",a+i);
sum+=a[i];
}
int m=(1<<n)-1;
for(int i=1; i<m; i++) {///枚举第一条边
all=0;
bn=0;
for(int j=0; j<n; j++) {///第二条与第三条边的和
if((1<<j)&i) {
all+=a[j];
b[bn++]=a[j];
}
}
largest=sum-all;
if(largest>=all)
continue;
smallest=all/2;
dfs(0,0);
}
printf("%d\n",ans);
}
return 0;
}

HDU 4277 USACO ORZ(暴力+双向枚举)的更多相关文章

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

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

  2. hdu 4277 USACO ORZ dfs+hash

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

  3. hdu 4277 USACO ORZ DFS

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

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

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

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

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

  6. hdu 4277 USACO ORZ

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

  7. hdu 4277 USACO ORZ (Dfs)

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

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

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

  9. hdu 5762 Teacher Bo 暴力

    Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

随机推荐

  1. [模板] 动态ST表

    ST表本身是不可修改的. 如果考虑增加一个数,可以把ST表反过来写,即f[i][j]表示i往前1<<j个数,一个数最多影响logn个数,常数非常小. #include<iostrea ...

  2. NTP服务和DNS服务

    1.NTP时间服务器 作用:NTP主要用于对计算机的时间同步管理操作 1.1  NTP部署 服务端:192.168.16.6 客户端:192.168.16.7 [root@localhost ~]# ...

  3. C语言之static用法

    1,static修饰全局变量 限定变量的作用域.被static修饰的全局变量存储域不变,依然存储在静态存储区,即bss段或data段.但作用域发生改变,被static修饰全局变量只能被本文件的函数访问 ...

  4. 分分钟钟学会Python - 模块

    目录 模块 1 模块基础知识 2 random 返回随机生成的一个实数 3 hashlib 摘要算法模块 Hmac算法 4 time /datetime 时间模块 5 getpass 密码不显示(只能 ...

  5. 教你轻松在React Native中集成统计(umeng)的功能(最新版)

    关于在react-native中快速集成umeng统计,网上的文章或者教程基本来自----贾鹏辉老师的文章http://www.devio.org/2017/09/03/React-Native-In ...

  6. reversing.kr replace 之write up

    好似reversing除了前几个容易些,后面的都很难的.看wp都很困难.首先wp提示crtl+n查看程序所有函数,并且找到了测试函数: 补充: GetDlgItemInt函数通过发送控件WM_GETT ...

  7. ES6(对象扩展)

    ES6(对象(object)新增特性) 1.简介表示法 o,k 为属性:这种为无方法的情况. 这种为有方法. 2.属性表达式 ES6中,[a]就是 b . 3.新增API 1.数组也是引用类型 2.O ...

  8. luogu2234 [HNOI2002]营业额统计

    treap水过 #include <iostream> #include <cstdlib> #include <cstdio> using namespace s ...

  9. Python中input()和raw_input()函数的区别

    问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...

  10. .net中的协变和逆变

    百度:委托中的协变和逆变. 百度:.net中的协变和逆变. 协变是从子类转为父类. 逆变是从父类到子类. 这样理解不一定严谨或者正确.需要具体看代码研究.