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. 周期性任务计划: Crontab

    文中部分内容摘自骏马金龙的博客,查看可点击 1. crontab简述 crontab命令用于周期性的执行任务,想要执行这个命令,需要首先启动crond(守护进程)服务才行 crontab是配置管理cr ...

  2. 如何用纯 CSS 创作一种按钮被瞄准的交互特效

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. 在线演示 https://codepen.io/zhang-ou/pen/ELWMLr 可交互视频教程 此视 ...

  3. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. LeetCode07--整数反转

    ''' 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 ...

  5. coraldraw快捷键

        显示导航窗口(Navigator window) [N] 运行 Visual Basic 应用程序的编辑器 [Alt]+[F11]  保存当前的图形 [Ctrl]+[S]  打开编辑文本对话框 ...

  6. python之Gui编程事件绑定 2014-4-8

    place() 相对定位与绝对定位 相对定位 拖动会发生变化 绝对定位不会from Tkinter import *root = Tk()# Absolute positioningButton(ro ...

  7. ubunt设置终端快捷键设置 及 常用快捷键

    Ctrl+A:将光标移动到命令行的开始处. Ctrl+E:将光标移动到命行令的结尾处. Ctrl+U:删除行首到光标出的字符. Ctrl+Z:把当前进程送到后台处理.  &    bg 部分快 ...

  8. 【数据传输 2】批量导入的前奏:将Excel表中的数据转换为DataTable类型

    导读:我们知道,在数据库中,数据集DataSet是由多张DataTable表组成.所以,如果我们需要将数据从外部导入到数据库中,那么要做的很重要的一步是将这些数据转换为数据库可以接受的结构.今天在用S ...

  9. Python()- 面向对象三大特性----封装

    封装: [封装]         隐藏对象的属性和实现细节,仅对外提供公共访问方式.[好处] 1. 将变化隔离: 2. 便于使用:3. 提高复用性: 4. 提高安全性:[封装原则]      1. 将 ...

  10. HDU 6396 贪心+优先队列+读入挂

    Swordsman Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...