HDU 4277 USACO ORZ(暴力+双向枚举)
USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3809 Accepted Submission(s): 1264
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.
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)
1
3
2 3 4
1
题意:给你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(暴力+双向枚举)的更多相关文章
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- hdu 4277 USACO ORZ dfs+hash
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 4277 USACO ORZ DFS
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 4277 USACO ORZ(dfs+剪枝)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- hdu 4277 USACO ORZ (dfs暴搜+hash)
题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...
- hdu 4277 USACO ORZ
没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- hdu 4277 USACO ORZ (Dfs)
题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...
- hdu 4277 USACO ORZ (暴力+set容器判重)
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 5762 Teacher Bo 暴力
Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
随机推荐
- saltstack快速部署
yum install wget deltarpm -y wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/rep ...
- root连接ubuntu18.04“拒绝访问”的解决方法
1.设置root账户 sudo passwd root 2.ssh远程登陆拒绝访问:修改SSH配置文件 sudo vim /etc/ssh/sshd_config 找到并用#注释掉这行:PermitR ...
- tomcat7使用dbcp连接池遇到的坑
项目部署在tomcat后每隔一段时间便会报错 Cause: java.sql.SQLException: Could not retrieve transation read-only status ...
- (二十二)python 3 sort()与sorted()
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...
- springMVC中处理静态资源的几种方案
处理静态资源方案一:在web.xml文件中配置如下: <!-- <!–解决静态资源方案–> <servlet-mapping> <servlet-name>d ...
- 阿里巴巴集团加入W3C,成为W3C会员
根据W3C官方推特最新消息:阿里巴巴集团正式加入W3C,成为W3C会员. W3C官方推特:https://twitter.com/w3c/status/566244180372889601 同时可以在 ...
- (转)]PYTHON Tkinter GUI
import Tkinterroot=Tkinter.Tk()label=Tkinter.Label(root,text='hello ,python')label.pack() #将LAB ...
- 分享14个很酷的jQuery导航菜单插件
导航按钮是网站的非常重要的一部分,因其将网站的所有部分而集中一处,jQuery导航菜单插件在其中扮演重要的角色. 本文介绍了14个很酷的jQuery导航菜单插件,它们够漂亮.简单,并且完全兼容各种类型 ...
- 大数据学习——hive的sql练习
1新建一个数据库 create database db3; 2创建一个外部表 --外部表建表语句示例: create external table student_ext(Sno int,Sname ...
- 大数据学习——mapreduce汇总手机号上行流量下行流量总流量
时间戳 手机号 MAC地址 ip 域名 上行流量包个数 下行 上行流量 下行流量 http状态码 1363157995052 13826544101 5C-0E-8B-C7-F1-E0:CMCC 12 ...