HDU 4277 USACO ORZ(暴力+双向枚举)
2017-06-06 11:21
369 查看
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 2012 ACM/ICPC Asia Regional Changchun Online
题意:给你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 (暴力搜索+set去重)
- HDU 4277 USACO ORZ [状态压缩+枚举]
- hdu 4277 USACO ORZ (暴力+set容器判重)
- 暴力枚举,dfs(三角形篱笆,HDU 4277)
- hdu 4277 USACO ORZ (暴力+set容器判重)
- HDU 5778 (压缩范围枚举暴力跑素数表)
- HDU 3697(H) ——Selecting courses(暴力枚举,贪心)
- HDU 5308 I Wanna Become A 24-Point Master (暴力枚举+构造)
- hdu 5128 The E-pang Palace (计算几何+暴力枚举)
- HDU 4277 USACO ORZ (dfs + hash)
- hdu 4968 Improving the GPA(暴力枚举)
- hdu 5128 The E-pang Palace(计算几何,暴力枚举)
- hdu 4770 Lights Against Dudely(暴力枚举dfs)
- hdu 6049 Sdjpx Is Happy(区间DP+暴力枚举)
- hdu 2363 Cycling (最短路spfa + 暴力枚举)
- HDU 5310 Hidden String(暴力枚举)
- hdu 5033 Building (单调栈 或 暴力枚举 )
- 暴力枚举,dfs(连连看,HDU 4272)
- hdu 4414 暴力枚举