您的位置:首页 > 其它

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;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: